using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static ulong[] NList => ReadLine().Split().Select(ulong.Parse).ToArray(); public static void Main() { Solve(); } static void Solve() { var c = NList; if (c[1] == 1) WriteLine(0); else if (c[0] == 0) WriteLine("NaN"); else { var (g, x, _) = XGcd(c[0], c[1]); if (g != 1) WriteLine("NaN"); else WriteLine((x + c[1]) % c[1]); } } // 拡張ユークリッド互除法 ax + by = gcd(a, b) を満たす x, y を求める public static (ulong g, ulong x, ulong y) XGcd(ulong a, ulong b) { ulong x0 = 1, y0 = 0, x1 = 0, y1 = 1; while (b != 0) { var q = a / b; var prevA = a; a = b; b = prevA % b; var prevX0 = x0; var prevY0 = y0; x0 = x1; x1 = prevX0 - q * x1; y0 = y1; y1 = prevY0 - q * y1; } return (a, x0, y0); } }