#include <bits/stdc++.h>
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define mset(x, y) memset(x, y, sizeof x)
#define mcpy(x, y) memcpy(x, y, sizeof x)
using namespace std;

typedef long long LL;
typedef pair <int, int> pii;

inline int Read()
{
	int x = 0, f = 1, c = getchar();
	for (; !isdigit(c); c = getchar())
		if (c == '-')
			f = -1;
	for (;  isdigit(c); c = getchar())
		x = x * 10 + c - '0';
	return x * f;
}

int main()
{
#ifdef wxh010910
	freopen("data.in", "r", stdin);
#endif
	int n = Read(), m = Read();
	if (n == 1)
	{
		while (m --)
			printf("%d\n", min(Read(), Read()));
		return 0;
	}
	while (m --)
	{
		int a = Read(), b = Read();
		while (a ^ b)
		{
			if (a < b)
				swap(a, b);
			a = (a + n - 2) / n;
		}
		printf("%d\n", a);
	}
	return 0;
}
