import std.algorithm, std.array, std.container, std.range; import std.string, std.conv; import std.numeric, std.math, std.bigint, std.bitmanip, std.random; import std.stdio, std.typecons; void main() { auto rd = readln.split.map!(to!int); auto m = rd[0], n = rd[1]; reduceFrac(m, n); auto r = 0; while (n > 1) { if (m > n) { r += m / n; m %= n; } else { r += 1; swap(m, n); } } writeln(r + m - 1); } void reduceFrac(ref int m, ref int n) { int g = gcd(m, n); m /= g; n /= g; }