#!/usr/bin/env python2 # coding: utf-8 def extgcd(a, b): x,y, u,v = 0,1, 1,0 while a != 0: q, r = b/a, b%a m, n = x-u*q, y-v*q b,a, x,y, u,v = a,r, u,v, m,n g = b return x, y, g a, b = 0, 1 def solve(): for i in xrange(3): na, nb = map(int,raw_input().split()) x, y, g = extgcd(b, nb) if a%g != na%g: print -1; return l = b/g*nb a, b = ((a+(na-a)/g*x*b)%l, l) if a == 0: a = b print a % 1000000007; return solve()