while 1:

    n,m=list(map(int,input().split()))

    if n==0:break

    a=m-1

    while a<m*n-n:a=m*a//(m-1)+1

    print((n*m-a))
while 1:

    n,m=list(map(int,input().split()))

    if n==0:break

    a=0

    for i in range(2,n+1):a=(a+m)%i

    print((a+1))
while 1:
    n, m = list(map(int, input().split()))
    if n == 0:
        break
    a = m - 1
    while a < m * n - n:
        a = m * a // (m - 1) + 1
    print((n * m - a))

while 1:
    n, m = list(map(int, input().split()))
    if n == 0:
        break
    a = 0
    for i in range(2, n + 1):
        a = (a + m) % i
    print((a + 1))

-    a = m - 1 -    while a < m * n - n: -        a = m * a // (m - 1) + 1 -    print((n * m - a)) +    a = 0 +    for i in range(2, n + 1): +        a = (a + m) % i +    print((a + 1))