f=open("vaipsis.txt") k, l, p=[int(s) for s in f.readline().strip().split(" ")] n1, s1, e1, w1=[int(s) for s in f.readline().strip().split(" ")] n2, s2, e2, w2=[int(s) for s in f.readline().strip().split(" ")] w1, l-e1 w2, l-e2 if w1w2: kattuvusx=min(l-e1, l-e2)-w2 else: if l-e1>w1: kattuvusx=min(l-e1, l-e2)-w1 print(kattuvusx)