s = input().split()
# 球队个数
n = int(s[0])
# 第一场
t = int(s[1])
# 定义队列a
a = [0] * (n + 1)
a[1] = t
# 队伍从1号开始
# 生成之后的数值
for i in range(2, n + 1):
    a[i] = ((a[i - 1] * 3703 + 1047) % n) + 1

# 定义记录获胜队伍得分数组
score = [0] * (n + 1)
# 要看连续几次,第一场
score[a[1]] += 1
# 第2场
if a[2] == a[1]:
    score[a[2]] += 2
else:
    score[a[2]] += 1
# 场次  i场次 a[i]第i场获胜的队伍编号  score[a[i]]获胜队伍的得分
for i in range(3, n + 1):
    if a[i] == a[i - 1] and a[i] == a[i - 2]:
        score[a[i]] += 3
    elif a[i] == a[i - 1]:
        score[a[i]] += 2
    else:
        score[a[i]] += 1

print(max(score))

/**************************************************************
	Problem: 1559
	User: admin
	Language: Python
	Result: Accepted
	Time:518 ms
	Memory:34480 kb
****************************************************************/