#define _CRT_SECURE_NO_WARNINGS 1
#define ll long long
#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<math.h>
#include<cstring>
#include<algorithm>
#include<functional>
#include<numeric>
using namespace std;
int n, m, c = 1, now = 0, x = 0;
int used[300], pre[300];
int find(int x)
{
return x == pre[x] ? x : pre[x] = find(pre[x]);
}
int main()
{
cin >> n >> m;
for (int i = 0; i <= n; ++i)pre[i] = i;
used[0] = 1;
while (now < m)
{
if (x > n)
{
x = 0;
continue;
}
x = find(x);
if (!used[x])
{
used[x] = 1;
pre[x] = x + 1;
if (++now == m)
{
cout << x;
break;
}
}
x += (c * c * c) % 5 + 2;
++c;
}
return 0;
}