#include <string.h>
#include <iostream>

using namespace std;

int main()
{
	const int MAX_DATAS = 1000000;

	int N[MAX_DATAS], M[MAX_DATAS], CHECK[1001], ANS[MAX_DATAS], C1 = 0, C = 1, D = 1, E = 0;

	while (true)
	{
		memset(CHECK, -1, sizeof(CHECK)); // -1

		C = 1; D = 1;

		cin >> N[C1] >> M[C1];

		if (N[C1] == 0 && M[C1] == 0) { goto Exit2; }

		while (true)
		{
			if (CHECK[C] % M[C1] != 0)
			{
				CHECK[C] = D; D++;
			}

			C++;

			if (C > N[C1]) { C -= N[C1]; }

			E = 0;

			for (int i = 1; i <= N[C1]; i++)
			{
				if (CHECK[i] % M[C1] != 0)
				{
					E++;
				}
			}

			if (E == 0) { goto Exit1; }
		}

	Exit1:;

		for (int i = 1; i <= N[C1]; i++)
		{
			CHECK[i] /= M[C1];
		}

		for (int i = 1; i <= N[C1]; i++)
		{
			if (CHECK[i] == N[C1]) { ANS[C1] = i; }
		}

		C1++;
	}

Exit2:;

	for (int i = 0; i < C1; i++)
	{
		cout << ANS[i] << endl;
	}

	return 0;
}