// // Created by zeronosu77108 on 2021/02/28. // import kotlin.collections.* import kotlin.math.* @kotlin.ExperimentalStdlibApi fun main() { val (x1, y1) = readLine()!!.split(" ").map { it.toLong() } val (x2, y2) = readLine()!!.split(" ").map { it.toLong() } val (x3, y3) = readLine()!!.split(" ").map { it.toLong() } val (ans, _) = crt(mutableListOf((x1 to y1), (x2 to y2), (x3 to y3))) println(ans ?: -1) } /** * Chinese remainder theorem() * @param am 連立合同式 z ≡ x (mod m1) : (x, m1) * @return (z, m) x ≡ z (mod m) */ fun crt(am : List>) : Pair { var r = 0L; var n = 1L for ((a, m) in am) { val (x, y, d) = ext_gcd(n, m); // x is inv of n/d (mod. m/d) if ((a - r) % d != 0L) return null to null val tmp = (a - r) / d * x % (m/d); r += n * tmp; n *= m / d; } return (r%n+n)%n to n } fun ext_gcd(a : Long, b : Long) : Triple { return when(b) { 0L -> Triple(1L,0L, a) else -> { val (x, y, g) = ext_gcd(b, a%b); return Triple(y , x - a/b * y, g) } } }