<script>
let x = 0, y = 0, x1 = 0, y1 = 0;
let x0 = 0, y0 = 0, g = 0;
function
gcd(a, b)
{
if
(b == 0)
{
x = 1;
y = 0;
return
a;
}
let store_gcd = gcd(b, a % b);
x = y1;
y = x1 - y1 * parseInt(a / b, 10);
return
store_gcd;
}
function
possible_solution(a, b, c)
{
g = gcd(Math.abs(a), Math.abs(b));
if
(c % g != 0)
{
return
0;
}
x0 *= parseInt(c / g, 10);
y0 *= parseInt(c / g, 10);
if
(a < 0)
x0 *= -1;
if
(b < 0)
y0 *= -1;
return
1;
}
function
shift_solution(a, b, shift_var)
{
x += shift_var * b;
y -= shift_var * a;
}
function
find_min_sum(a, b, c)
{
let x = 0, y = 0, g = 0;
if
(possible_solution(a, b, c) == 0)
return
-1;
if
(g != 0)
{
a = parseInt(a / g, 10);
b = parseInt(b / g, 10);
}
let sign_a = a > 0 ? +1 : -1;
let sign_b = b > 0 ? +1 : -1;
shift_solution(a, b, parseInt(-x / b, 10));
if
(x < 0)
shift_solution(a, b, sign_b);
let minx1 = x;
shift_solution(a, b, parseInt(y / a, 10));
if
(y < 0)
shift_solution(a, b, -sign_a);
let minx2 = x;
if
(minx2 > x)
{
let temp = minx2;
minx2 = x;
x = temp;
}
let minx = Math.max(minx1, minx2);
if
(minx > x)
return
-1;
let miny = parseInt((c - a * x) / b, 10);
return
(miny + minx);
}
let a = 2, b = 2, c = 0;
document.write(find_min_sum(a, b, c));
</script>