#include <iostream>
#include <cstring>
using namespace std;
const int N = 100010;
int n, k;
int d[N], q[N];
void bfs()
{
int hh = 0, tt = -1;
memset(d, -1, sizeof d);
q[ ++ tt] = 0;
d[0] = 0;
while (hh <= tt)
{
auto t = q[hh ++ ];
int a = (t + 1) % n;
if (d[a] == -1)
{
d[a] = d[t] + 1;
q[ ++ tt] = a;
}
int b = (t + k) % n;
if (d[b] == -1)
{
d[b] = d[t] + 1;
q[ ++ tt] = b;
}
}
}
int main()
{
scanf("%d%d", &n, &k);
bfs();
int res = 0;
for (int i = 0; i < n; i ++ )
res = max(res, d[i]);
cout << res << endl;
return 0;
}