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