# -*- coding: utf-8 -*-

"""
45题， 具体解法看书
"""


def lastRemaining(n, m):
	if n < 1 or m < 1:
		return -1

	last = 0

	for i in xrange(2, n+1):
		last = (last + m) % i

	return last


if __name__ == "__main__":
	print lastRemaining(5, 3)
9fe7acd7d336d5a5605e2339766e51c8bcdbdc40
# -*- coding: utf-8 -*-

"""
45题， 具体解法看书
"""


def lastRemaining(n, m):
	if n < 1 or m < 1:
		return -1

	last = 0

	for i in xrange(2, n+1):
		last = (last + m) % i

	return last


if __name__ == "__main__":
	print lastRemaining(5, 3)