Problem:
 Score : 100 points    Problem Statement Takahashi is drawing a segment on grid paper.  From a certain square, a square that is x squares to the right and y squares above, is denoted as square (x, y) .  When Takahashi draws a segment connecting the lower left corner of square (A, B) and the lower left corner of square (C, D) , find the number of the squares crossed by the segment.  Here, the segment is said to cross a square if the segment has non-empty intersection with the region within the square, excluding the boundary.      Constraints  1 \leq A, B, C, D \leq 10^9  At least one of A \neq C and B \neq D holds.         Input The input is given from Standard Input in the following format:  A  B  C  D       Output Print the number of the squares crossed by the segment.        Sample Input 1 1 1 3 4      Sample Output 1 4       Sample Input 2 2 3 10 7      Sample Output 2 8

Solution (Python):
from math import gcd
a,b,c,d=map(int,input().split())
if a==c or b==d:
    print(0)
    exit()
p=abs(a-c)
q=abs(b-d)
g=gcd(p,q)
p//=g
q//=g
print((p+q-1)*g)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

a, b, c, d = map(int, input().split())

if a == c or b == d:
    print(0)
    sys.exit(0)

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)

x = abs(a - c)
y = abs(b - d)

_gcd = gcd(x, y)
x2 = x // _gcd
y2 = y // _gcd

import math

print((x2 + y2 - 1) * _gcd)

Solution (Python):
a,b,c,d=map(int,input().split())
gcd=lambda a,b:gcd(b,a%b)if a%b else b
def 数学は最強也(a,b):
  q=gcd(a,b)
  return q*((a//q)+(b//q-1))
print(数学は最強也(abs(a-c),abs(b-d)))

Solution (Python):
a,b,c,d = map(int, input().split())
def gcd(u,v):
  while v: u,v = v,u%v
  return u
s = abs(c-a)
t = abs(d-b)
u = gcd(s,t)
print(u*((s+t)//u-1) if u else 0)

Solution (Python):
from fractions import gcd
a,b,c,d=map(int,input().split())
if abs(c-a)==0 or abs(d-b)==0:
  print(0)
else:
  x,y=abs(c-a),abs(d-b)
  g=gcd(x,y)
  print(x+y-g)

Solution (Python):
from fractions import gcd
a, b, c, d = map(int, input().split())
ac = abs(a - c)
bd = abs(b - d)
if ac == bd:
  ans = ac
elif ac == 0 or bd == 0:
  ans = 0
else:
  ans = ac + bd - gcd(ac, bd)

print(ans)

Solution (Python):
from fractions import gcd
import math
a, b, c, d = [int(item) for item in input().split()]
a = abs(a - c)
b = abs(b - d)
if a > b:
    a, b = b, a
g = gcd(a, b)
a //= g; b //= g

print((a + b - 1) * g)

Solution (Python):
import fractions
A, B, C, D = map(int, input().split())
X = abs(C-A)
Y = abs(D-B)
if X == 0 or Y == 0:
  print(0)
else:
  
  g = fractions.gcd(X, Y)
  print(X+Y-g)

Solution (Python):
def gcd(a,b):
    if a < b:
        a, b = b, a
    while b >= 1:
        a, b = b, a % b
    return a

a, b, c, d = [ int(v) for v in input().split() ]

if a == c:
    print(abs(b-d))
elif b == d:
    print(abs(a-c))
else:
    x, y = max(abs(a-c),abs(b-d)),  min(abs(a-c),abs(b-d))
    a = gcd(x,y)
    ux, uy = x // a, y // a

    print(a * (ux+uy-1))

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

from fractions import gcd

A,B,C,D = map(int,input().split())

dx = abs(A-C)
dy = abs(B-D)

g = gcd(dx,dy)
answer = (dx + dy) - g
print(answer)

Solution (Python):
def gcd(x,y):
    if x%y==0:
        return y
    while x%y>0:
        x,y=y,x%y
    return y
a,b,c,d=map(int,input().split())
x,y=abs(a-c),abs(b-d)
g=gcd(x,y)
print(g*(x//g+y//g-1))

Solution (Python):
import fractions
from functools import reduce
def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


a,b,c,d = map(int,input().split())

c -= a
d -= b

c = abs(c)
d = abs(d)

ll = gcd(c,d)



k = d/c
co = 0
kkk = 0


print((c//ll + d//ll - 1)*ll)

Solution (Python):
a,b,c,d = (int(i) for i in input().split())
def gcd(x,y):
	if x%y==0: return y
	else: return gcd(y,x%y)
if a==c or b==d: ans = 0
else:
	n = gcd(abs(a-c),abs(b-d))
	e,f = abs(a-c)//n,abs(b-d)//n
	ans = (e+f-1)*n
print(ans)

Solution (Python):
import fractions
x1,y1,x2,y2 = map(int,input().split())
if x1 == x2 or y1 == y2:
    print(0)
    exit()

dx,dy = abs(x1-x2),abs(y1-y2)
g = fractions.gcd(dx,dy)
print(g * (dx//g + dy//g - 1))

Solution (Python):
import fractions
if __name__ == "__main__":
    A, B, C, D = map(int, input().split())
    A -= C
    B -= D
    W = abs(A)
    H = abs(B)
    if W == 0 or H == 0:
        print(0)
        exit()
    res = W + H - 1
    g = fractions.gcd(W, H)
    print(res - g + 1)

Solution (Python):
def gcd(a,b): return b if a%b==0 else gcd(b,a%b)
def main():
    a,b,c,d = map(int,input().split())
    h = abs(b-d)
    w = abs(c-a)
    if h==0 or w==0 : return 0
    m = gcd(max(h,w),min(h,w))
    return h+w-m

print(main())

Solution (Python):
import fractions

def main():
    a,b,c,d = map(int,input().split())
    a,c = min(a,c),max(a,c)
    b,d = min(b,d),max(b,d)

    c -= a
    a = 0
    d -= b
    b = 0

    if a==c or b==d:
        return 0

    g = fractions.gcd(c,d)
    c//=g
    d//=g
    ct = c+d-1
    return g*ct

if __name__ == '__main__':
    print(main())

Solution (Python):
import fractions
a,b,c,d=map(int,input().split())
c=abs(c-a)
d=abs(d-b)
g=fractions.gcd(c,d)
j=int(c/g)
k=int(d/g)
print((j+k-1)*g)

Solution (Python):
import fractions
a,b,c,d=map(int,input().split())
c=abs(c-a)
d=abs(d-b)
g=fractions.gcd(max(c,d),min(c,d))
j=int(c/g)
k=int(d/g)
print((j+k-1)*g)

Solution (Python):
import fractions
a,b,c,d=map(int,input().split())
x=abs(c-a)
y=abs(d-b)
if x==0 or y==0:
    print(0)
else:
    gcd=fractions.gcd(x,y)
    m=x//gcd
    n=y//gcd
    print((m+n-1)*gcd)

Solution (Python):

import fractions
a, b, c, d = map(int, input().split())
h = abs(a - c)
w = abs(b - d)
if h == 0 or w == 0:
    ans = 0
else:
    h1 = h // fractions.gcd(h, w)
    w1 = w // fractions.gcd(h, w)
    ans = (h1 + w1 - 1) * fractions.gcd(h, w)
print(ans)

Solution (Python):
a,b,c,d=map(int,raw_input().split())
g=lambda m,n:g(n,m%n)if m%n else n
A=map(abs,[c-a,d-b])
A.sort()
print sum(A)-g(*A)

Solution (Python):
import math
import fractions
A, B, C, D = map(int, input().split())
if A == C or B == D:
    print(0)
    exit()

x = abs(A - C)
y = abs(B - D)

print(x + y - fractions.gcd(x, y))

Solution (Python):
def gcd(a,b):
	if b==0:return a
	return gcd(b,a%b)
a,b,c,d=map(int,input().split())
c=abs(c-a)
d=abs(d-b)
g=gcd(max(c,d),min(c,d))
j=int(c/g)
k=int(d/g)
print((j+k-1)*g)

Solution (Python):
from fractions import gcd
a, b, c, d = map(int, raw_input().split())
x, y = abs(c-a), abs(d-b)
if (x == 0 or y == 0):
    print 0
else:
    print x+y-gcd(x,y)

Solution (Python):
a,b,c,d = map(int, raw_input().split())

x = abs(c-a)
y = abs(d-b)

if x == 0 or y == 0:
	print 0
	import sys
	sys.exit(0)

def gcd(a,b):
	return a if b == 0 else gcd(b, a % b)

e = gcd(x,y)
x /= e
y /= e

print (x+y-1) * e