n = read_line.to_i64 e = read_line.to_i if e == 0 puts 0 exit end ab = n.abs a = Array.new(e) do v = ab % 5 ab //= 5 n < 0 ? (5 - v) % 5 : v end if a[0] == 2 || a[0] == 3 puts "NaN" exit end if a[0] == 0 q = [[0]] elsif a[0] == 1 q = [[1], [4]] else q = [[2], [3]] end 1.upto(e - 1) do |i| nq = [] of Array(Int32) q.each do |cand| sum = 0 1.upto(i - 1) do |j| sum += cand[j] * cand[i - j] end 5.times do |j| if (j * cand[0] * 2 + sum) % 5 == a[i] nq << cand + [j] end end end if nq.empty? puts "NaN" exit end q = nq end ans = 0i64 base = 1i64 e.times do |i| ans += base * q[0][i] base *= 5 end puts ans