import math t=input() x,a=list(map(int,input().split())) y,b=list(map(int,input().split())) gcd=math.gcd(a-x,b+y) print(t%gcd)