w = int(input()) d = int(input()) if d == 1: print(w) else: while True: a=w//(d**2) w = w-a d=d-1 if d == 1: break print(w)