Harryxun/stf_run huggingface dataset:
N = input()

k = N // 3
l = N // 5
m = N // 15

sum = (N * (N + 1) / 2) - (3 * (-1 + 3**k)/2) - (5 * (-1 + 5**l)/4) + (15 * (-1 + 15**k)/14)
print(sum)

### FIXED CODE ###

N = int(input())
 
k = N // 3
l = N // 5
m = N // 15
 
sum = (N * (N + 1) / 2) - (3* k * (k + 1) / 2) - (5* l * (l + 1) / 2) + (15* m * (m + 1) / 2)
print(int(sum))
#include <bits/stdc++.h>
using namespace std;
int main()
{
  int N;
  cin >> N;
  int count= 0;
  
  vector<int> p(N);
  for (int i=1 ;i < N ;i++) {
    cin >> p[i];
    if (p[i-1]+1 != p[i]) {
      count++;
      }
    }
  
  if (count == 0 || count ==2){
    cout << "YES" << endl;
  }
  else cout << "NO" << endl;
}


### FIXED CODE ###

n = int(input())
li = list(map(int,input().split()))
count = 0
for i in range(n):
  if(li[i] != i+1):
    count += 1
if(count == 0 or count == 2):
  print("YES")
else:
  print("NO")
from collections import deque

H,W=map(int,input().split())
c=[['#']*(W+2)]
ten=0
for i in range(H):
    ip=list(input())
    ten+=ip.count('.')
    c.append(['#']+ip+['#'])
c.append(['#']*(W+2))
Q=deque([[1,1,1]])

def move(yxc):
    global Q,c
    y,x,cost=yxc[0],yxc[1],yxc[2]
    if c[y+1][x]=='.':
        c[y+1][x]=cost+1
        Q.append([y+1,x,cost+1])
    if c[y-1][x]=='.':
        c[y-1][x]=cost+1
        Q.append([y-1,x,cost+1])
    if c[y][x+1]=='.':
        c[y][x+1]=cost+1
        Q.append([y,x+1,cost+1])
    if c[y][x-1]=='.':
        c[y][x-1]=cost+1
        Q.append([y,x-1,cost+1])

c[1][1]=1
while len(Q)>0:
    move(Q.popleft())

goal=c[H][W]
print(ten-goal)

### FIXED CODE ###

from collections import deque

H,W=map(int,input().split())
c=[['#']*(W+2)]
ten=0
for i in range(H):
    ip=list(input())
    ten+=ip.count('.')
    c.append(['#']+ip+['#'])
c.append(['#']*(W+2))
Q=deque([[1,1,1]])

def move(yxc):
    global Q,c
    y,x,cost=yxc[0],yxc[1],yxc[2]
    if c[y+1][x]=='.':
        c[y+1][x]=cost+1
        Q.append([y+1,x,cost+1])
    if c[y-1][x]=='.':
        c[y-1][x]=cost+1
        Q.append([y-1,x,cost+1])
    if c[y][x+1]=='.':
        c[y][x+1]=cost+1
        Q.append([y,x+1,cost+1])
    if c[y][x-1]=='.':
        c[y][x-1]=cost+1
        Q.append([y,x-1,cost+1])

c[1][1]=1
while len(Q)>0:
    move(Q.popleft())

goal=c[H][W]
if goal=='.' or goal=='#':
    print(-1)
else:
    print(ten-goal)
N = int(input())
S, T = map(str,input().split())
for i in N:
     print(S[i] + T[i],end="")


### FIXED CODE ###

N = int(input())
S, T = map(str,input().split())
for i in range(N):
     print(S[i] + T[i],end="")
from collections import deque

N = int(input())
A = list(map(int, input().split()))

q = deque()
q.append(A[0])
q.append(A[1])
for i in range(2, N):
  a = A[i]
  if i % 2 == 1:
    q.appendleft(a)
  else:
    q.append(a)

print(" ".join(map(str, q)))


### FIXED CODE ###

from collections import deque

N = int(input())
A = list(map(int, input().split()))

q = deque()
for i in range(N):
  a = A[i]
  if i > 1 and i % 2 == 0:
    q.appendleft(a)
  else:
    q.append(a)

if N % 2 == 0:
  q.reverse()
print(" ".join(map(str, q)))
import math
A,B=map(int,input().split())

print((A * B) // math.gcd(A, B))

### FIXED CODE ###

import fractions
A,B=map(int,input().split())

print((A * B) // fractions.gcd(A, B))
n,m = map(int,input().split())
ans = n *m
if ans <1000000000000000000:
  print(ans)
else:
  print("-1")

### FIXED CODE ###

n = int(input())
num_list=list(map(int,input().split()))
ans = 1
flg = True
if 0 in num_list:
  print("0")
else:
  for i in range(n):
    ans = ans*num_list[i]
  
    if ans > 10**18:
      print("-1")
      flg = False
      break
  if flg:
    print(ans)
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right

# https://atcoder.jp/contests/abc095/tasks/arc096_a
A, B, C, X, Y = read_ints()

# もし組み合わせて買ったほうがいいなら限界まで組み合わせて買う
buyed = min(X, Y)
c1 = min(A + B, 2 * C) * buyed
X -= buyed
Y -= buyed

# 残りの枚数はどうする?
if X:  # もしまだAピザを買う必要があれば、
    c2 = min(A, 2 * C) * X

if Y:  # もしまだAピザを買う必要があれば、
    c2 = min(B, 2 * C) * Y

print(c1 + c2)


### FIXED CODE ###

# https://atcoder.jp/contests/abc095/tasks/arc096_a


# A1枚→A円
# B1枚→B円
# A,B両方一枚→AB円


# もし2AB<=A+Bならmin(X,Y)になるまで買ったほうが安い
# Aだけ必要→A<AB→Aを買う、違うならABを買う
# Bだけ必要→上に同じ

# もし2AB<=A+Bでなかった場合
# A,B<ABの場合→A,Bを個別に買う
# A<2AB の場合→Aは個別で
# この議論は上に統合される

a, b, c, x, y = map(int, input().split())
# 両方買うほうがお得のときは先に買っておく
ans = 0
if 2 * c <= a + b:
    n = min(x, y)
    ans += 2 * n * c
    x -= n
    y -= n

# print(x, y, ans)

# Aだけ買う
if a <= 2 * c:
    ans += x * a
else:
    ans += x * 2 * c

if b <= 2 * c:
    ans += y * b
else:
    ans += y * 2 * c
print(ans)
K = int(input())
ans = 0
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            if j < k:
                j,k = k,j
            temp = gcd(j,k)
            if i < temp:
                i,temp = temp,i
            ans += gcd(i,temp)
print(ans)

### FIXED CODE ###

import math
 
k = int(input()) + 1
ans = 0
 
for a in range(1, k):
    for b in range(1, k):
        d = math.gcd(a, b)
        for c in range(1, k):
            ans += math.gcd(d, c)
 
print(ans)
n=int(input())
li=[list(map(int,input().split())) for i in range(n)]
for i in range(n):
  li[i].append(li[i][0]-li[i][1])
  li[i].append(li[i][0]+li[i][1])

from operator import itemgetter
li=sorted(li, key=itemgetter(3))

ans=0
now=0

for i in range(n-1):
  if li[now][3]>=li[i+1][3]: #今調査してるやつが、次のやつを覆ってしまってる場合
    ans+=1
    now=i
  elif li[now][3]>li[i+1][2]:
    ans+=1
  else:
    now+=i

print(n-ans)

### FIXED CODE ###

n=int(input())
li=[list(map(int,input().split())) for i in range(n)]
for i in range(n):
  li[i].append(li[i][0]-li[i][1])
  li[i].append(li[i][0]+li[i][1])

from operator import itemgetter
li=sorted(li, key=itemgetter(3))

ans=0
now=0

for i in range(n-1):
  if li[now][3]>=li[i+1][3]: #今調査してるやつが、次のやつを覆ってしまってる場合
    ans+=1
    now=i+1
  elif li[now][3]>li[i+1][2]:
    ans+=1
  else:
    now=i+1

print(n-ans)
from sys import stdin
input = stdin.readline
count = 0
def solve():
    n,m = map(int,input().split())
    p = [tuple(map(int,inp.split())) for inp in stdin.read().splitlines()]
    father = [-1] * n    # store the opposite of members in the union
    count = n
    def getfather(x):
        if father[x] < 0: return x
        father[x] = getfather(father[x])
        return father[x]


    def union(x, y):
        x = getfather(x)
        y = getfather(y)
        nonlocal count
        if x != y:
            count -= 1
            if father[x] > father[y]:
                x,y = y,x
            father[x] += father[y]
            father[y] = x
    for a,b in p:
        union(a,b)
    print(count - 1)

if __name__ == '__main__':
    solve()

### FIXED CODE ###

from sys import stdin
input = stdin.readline

def solve():
    n,m = map(int,input().split())
    p = [tuple(map(int,inp.split())) for inp in stdin.read().splitlines()]
    father = [-1] * n
    count = n
    def getfather(x):
        if father[x] < 0: return x
        father[x] = getfather(father[x])
        return father[x]


    def union(x, y):
        x = getfather(x)
        y = getfather(y)
        nonlocal count
        if x != y:
            count -= 1
            if father[x] > father[y]:
                x,y = y,x
            father[x] += father[y]
            father[y] = x

    for a,b in p:
        union(a-1,b-1)
        
    print(count - 1)

if __name__ == '__main__':
    solve()
a,b,c=input()
print("Yes" if a==2 else No)

### FIXED CODE ###

a,b,c=input()
print("Yes" if a==c else "No")
N, K =map(int, input().split())
import math
print(math.gcd(N,K))

### FIXED CODE ###

N, K =map(int, input().split())
ans=N%K
print(min(ans, K-ans))
import math
n=int(input())
kouho = []
ans = []
for i in range(1,math.floor(math.sqrt(n))):
    if n % i == 0 :
        kouho.append([i,n//i])
for j in range(len(kouho)):
    ans.append(kouho[j][0]+kouho[j][1]-2)
print(min(ans))

### FIXED CODE ###

import math
n=int(input())
kouho = []
ans = []
for i in range(1,math.ceil(math.sqrt(n)+1)):
    if n % i == 0 :
        kouho.append([i,n//i])
for j in range(len(kouho)):
    ans.append(kouho[j][0]+kouho[j][1]-2)
print(min(ans))
n = int(input())
a = list(map(int, input().split()))

if n == 1:
    print(a[0])
    exit()

b = list(sorted(set(a)))

def count_inversion(ans):
    s = [0 for _ in range(2 * n) + 2]

    def bit_add(i):
        x = i
        while x < 2 * n:
            s[x] += 1
            x += x & -x

    def bit_sum(i):
        x = i
        ret = 0
        while x > 0:
            ret += s[x]
            x -= x & -x
        return ret

    temp = [n for _ in range(n + 1)]
    temp[0] = n + 1
    for i in range(n):
        if a[i] >= b[ans]:
            temp[i + 1] = temp[i] + 1
        else:
            temp[i + 1] = temp[i] - 1
    res = 0
    for i in range(n + 1):
        x = temp[i]
        res += bit_sum(x)
        bit_add(x)
    return res

m = n * (n + 1) // 2

ans = 0

l = 0
r = len(b)
while 1:
    ans = (l + r) // 2
    if count_inversion(ans) * 2 >= m:
        l = ans
    else:
        r = ans
    if r - l <= 1:
        ans = l
        break

print(b[ans])


### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))

if n == 1:
    print(a[0])
    exit()

b = list(sorted(set(a)))

def count_inversion(ans):
    s = [0 for _ in range(2 * n + 2) ]

    def bit_add(i):
        x = i
        while x < 2 * n:
            s[x] += 1
            x += x & -x

    def bit_sum(i):
        x = i
        ret = 0
        while x > 0:
            ret += s[x]
            x -= x & -x
        return ret

    temp = [n for _ in range(n + 1)]
    temp[0] = n + 1
    for i in range(n):
        if a[i] >= b[ans]:
            temp[i + 1] = temp[i] + 1
        else:
            temp[i + 1] = temp[i] - 1
    res = 0
    for i in range(n + 1):
        x = temp[i]
        res += bit_sum(x)
        bit_add(x)
    return res

m = n * (n + 1) // 2

ans = 0

l = 0
r = len(b)
while 1:
    ans = (l + r) // 2
    if count_inversion(ans) * 2 >= m:
        l = ans
    else:
        r = ans
    if r - l <= 1:
        ans = l
        break

print(b[ans])
X = [for int(i) in input().split()]
for c, x in X:
  if x == 0:
  	print(c)

### FIXED CODE ###

X = [int(i) for i in input().split()]
for c, x in enumerate(X):
  if x == 0:
  	print(c+1)
def resolve():
    a_list = list(map(int,input().split()))
    b_list = list(map(int,input().split()))
    c_list = list(map(int,input().split()))
    arr = np.array([a_list, b_list, c_list])

    n = int(input())
    for _ in range(n):
        num = int(input())
        arr = np.where(arr == num, 0, arr)

    if True in np.all(arr == 0, axis=0):
        print('Yes')
        return

    elif True in np.all(arr == 0, axis=1):
        print('Yes')
        return

    elif arr[0, 0] == 0 and arr[1, 1] == 0 and arr[2, 2] == 0:
        print('Yes')
        return

    elif arr[2, 0] == 0 and arr[1, 1] == 0 and arr[0, 2] == 0:
        print('Yes')
        return

    else:
        print('No')
        return
      
resolve()

### FIXED CODE ###

import numpy as np


def resolve():
    a_list = list(map(int,input().split()))
    b_list = list(map(int,input().split()))
    c_list = list(map(int,input().split()))
    arr = np.array([a_list, b_list, c_list])

    n = int(input())
    for _ in range(n):
        num = int(input())
        arr = np.where(arr == num, 0, arr)

    if True in np.all(arr == 0, axis=0):
        print('Yes')
        return

    elif True in np.all(arr == 0, axis=1):
        print('Yes')
        return

    elif arr[0, 0] == 0 and arr[1, 1] == 0 and arr[2, 2] == 0:
        print('Yes')
        return

    elif arr[2, 0] == 0 and arr[1, 1] == 0 and arr[0, 2] == 0:
        print('Yes')
        return

    else:
        print('No')
        return
      
resolve()
x=int(input())
if x==1:
  print("0")
else:
  print("1!)

### FIXED CODE ###

x=int(input())

if x==1:
  print("0")
else:
  print("1")
# N = int(input())
# A = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]

N, K = [int(x) for x in input().split()]
V = [int(x) for x in input().split()]

import itertools

RV = list(itertools.accumulate(V))

# 左から最大でl個とる
# for l in range(K + 1):
#     # 右からは最大でk-l個
#     for r in range(K + 1 - l):
#         print("l:{}, r:{}".format(l, r))


# heapq.heappush(Q,)
import heapq

from collections import defaultdict


def f(k):
    ret = -99999999999999999999999
    dd = defaultdict(lambda: 0)
    s = 0
    for i in range(k):
        s += V[-k + i]
        dd[V[-k + i]] += 1

    ret = max(ret, s - neg(dd, K - k))

    for i in range(k):
        rm = V[-k + i]
        ad = V[-k + i + k]
        dd[rm] -= 1
        dd[ad] += 1
        s -= rm
        s += ad
        ret = max(ret, s - neg(dd, K - k))
        # print(ret)

    return ret


def neg(d, n):
    L = sorted(d.items())
    ret = 0

    for l in L:
        v, cnt = l[0], l[1]
        if n <= 0 or v >= 0:
            break

        if n >= cnt:
            ret += (v * cnt)
        else:
            ret += (v * n)
        n -= cnt
    return ret


ans = 0
for i in range(1, K + 1):
    ans = max(ans, f(i))
print(ans)


### FIXED CODE ###

# N = int(input())
# A = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]

N, K = [int(x) for x in input().split()]
V = [int(x) for x in input().split()]

ans = 0
# 左からl個(0..K)
for l in range(K + 1):
    # 右からはr個(0..K-l)
    for r in range(K - l + 1):
        # N個よりは取れない
        if l + r > N:
            continue
        # 残りの操作数
        d = max(0, K - (l + r))

        # 合計値
        s = sum(V[:l]) + sum(V[N - r:])
        # 手元のマイナス
        neg = filter(lambda x: x < 0, V[:l] + V[N - r:])
        neg = list(neg)
        neg.sort()
        for i in range(d):
            if i < len(neg):
                s -= neg[i]

        # print(s)
        ans = max(ans,s)

print(ans)
n = int(input().split())
d = list(map(int(input().split())))
print((sum(d)**2-sum(d[i]**2 for i in range(n)))//2)

             

### FIXED CODE ###

n = int(input())
d = list(map(int,input().split()))
print((sum(d)**2-sum(d[i]**2 for i in range(n)))//2)
n, m = [int(x) for x in input().split()]
lst = [None]*(n+1)
a = 0
b = 0
for i in range(m):
  p, s = input().split()
  if s == 'AC' and lst[p] is None:
    a += 1
    lst[p] = True
  elif s == 'WA' and lst[p] is None:
    b += 1
print(a,b)

### FIXED CODE ###

n, m = [int(x) for x in input().split()]
lst = [0]*(n+1)
a = 0
b = 0
for i in range(m):
  p, s = input().split()
  p = int(p)
  if s == 'AC' and lst[p] is not None:
    a += 1
    b += lst[p]
    lst[p] = None
  elif s == 'WA' and lst[p] is not None:
    lst[p] += 1
print(a,b)
import statistics
lis = []
for i in range(3):
  a = int(input())
  a = [a]
  lis += a
x = min(lis)
y = statistics.median(lis)
print(x+y)

### FIXED CODE ###

a, b, c = [int(i) for i in input().split()]
d = a + b
e = b + c
f = c + a
print(min(d, e, f))
from heapq import heappush, heappop

n, m = map(int, input().split())
A = input().split()
hq = []
for a in A:
    heappush(hq, -1 * int(a))

for _ in range(m):
    tmp = -1 * heappop(hq)
    print(hq)
    heappush(hq, -1 * (tmp // 2))

print(-sum(hq))

### FIXED CODE ###

from heapq import heapify, heappush, heappop

n, m = map(int, input().split())
A = list(map(lambda x: int(x) * -1, input().split()))
heapify(A)

for _ in range(m):
    tmp = -1 * heappop(A)
    heappush(A, -1 * (tmp // 2))

print(-sum(A))
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
uf = UnionFind(N)
for _ in range(M):
    a, b = map(int, input().split())
    uf.union(a-1, b-1)
print(len(uf.roots())-1)

### FIXED CODE ###

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
N,M=map(int,input().split())
uf = UnionFind(N)
for _ in range(M):
    a, b = map(int, input().split())
    uf.union(a-1, b-1)
print(len(uf.roots())-1)
s=list(input())
n=len(s)
cnt=0
for i in range(n//2):
  if s[i] != s[n-i]: cnt+=1
print(cnt)

### FIXED CODE ###

s=list(input())
n=len(s)
cnt=0
for i in range(n//2):
  if s[i] != s[n-i-1]: cnt+=1
print(cnt)
dic = {'A':10,'B':11,'C':12,'D':13,'E':14,'F':15}
x,y = input().split()
it dic[x] > dic[y]:
  print('>')
elif dic[x] < dic[y]:
  print('<')
else:
  print('=')

### FIXED CODE ###

dic = {'A':10,'B':11,'C':12,'D':13,'E':14,'F':15}
x,y = input().split()
if dic[x] > dic[y]:
  print('>')
elif dic[x] < dic[y]:
  print('<')
else:
  print('=')
import math

r = input()

print(2 * math.pi * r)


### FIXED CODE ###

import math
r = int(input())
print(2 * math.pi * r)
quit()
#ABC73
n = input()
if(n[0]=="9" or n[1]=="9"):
    print(Yes)
else:
    print(No)



### FIXED CODE ###

#ABC73
n = input()
if(n[0]=="9" or n[1]=="9"):
    print("Yes")
else:
    print("No")
n = int(input())
verdict = {'AC': 0, 'WA': 0, 'TLE': 0, 'RE': 0}

for _ in range(n):
    S = input()
    verdict[S.upper()] += 1
for i, j in verdict:
    print(f'{i} x {j}')


### FIXED CODE ###

n = int(input())
verdict = {'AC': 0, 'WA': 0, 'TLE': 0, 'RE': 0}

for _ in range(n):
    S = input()
    verdict[S.upper()] += 1
for i, j in verdict.items():
    print(f'{i} x {j}')
N = int(input())

if N%2==0:
  print(N)

else:
  print(2 ＊ N)


### FIXED CODE ###

n = int(input())

if n%2==0:
    print(n)

else:
    print(2 * n)
N = int(input())
T = list(map(int,input().split()))
M = int(input())
grid = []
for i in range(M):
    data = list(map(int,input().split()))
    grid.append(data)
fsum = sum(T)

for i in range(M):
    dsum = 0
    dsum = fsum - T[i] + grid[i][1]
    print(dsum)
    





### FIXED CODE ###

N = int(input())
T = list(map(int,input().split()))
M = int(input())
grid = []

for i in range(M):
    data = list(map(int,input().split()))
    grid.append(data)
fsum = sum(T)
for i in range(M):
##    print("gridi0="+str(grid[i][0]))
##    print("grioi1="+str(grid[i][1]))
##    print("T="+str(T[grid[i][0]-1]))
    dsum = fsum - T[grid[i][0]-1] + grid[i][1]
    print(dsum)
def f(x, m):
    return (x ** 2) % m


def main():
    n, x, m = map(int, input().split())
    lst = [[False, 0] for _ in range((m + 1))]
    lst[x] = [True, 1]
    res = [x]

    for i in range(2, m + 1):
        x = f(x, m)
        if lst[x][0] == False:
            lst[x][0] = True
            lst[x][1] = i
            res.append(x)
        else:
            loop = i - lst[x][1]
            non_loop = lst[x][1] - 1
            break

    ans = sum(res[:non_loop])
    ans += (n - 2) // loop * sum(res[non_loop:])
    ans += sum(res[non_loop:non_loop + (n - 2) % loop])
    print(ans)


if __name__ == "__main__":
    main()


### FIXED CODE ###

def f(x, m):
    return (x ** 2) % m


def main():
    n, x, m = map(int, input().split())
    lst = [[False, 0] for _ in range((m + 1))]
    lst[x] = [True, 1]
    res = [x]
    loop = 0

    for i in range(2, m + 1):
        x = f(x, m)
        if lst[x][0] == False:
            lst[x][0] = True
            lst[x][1] = i
            res.append(x)
        else:
            loop = i - lst[x][1]
            non_loop = lst[x][1] - 1
            break
    if loop == 0:
        print(sum(res[:n]))
        exit()
    ans = sum(res[:non_loop])
    ans += (n - non_loop) // loop * sum(res[non_loop:])
    ans += sum(res[non_loop:non_loop + (n - non_loop) % loop])
    print(ans)


if __name__ == "__main__":
    main()
import numpy
n = int(input())
s = input()
a = [0]*n
ans = 10**6
for i in range(n):
    if s[i] == 'W':
        a[i] = 1
res = numpy.cumsum(a)
for i in range(n):
    x = res[i]-str(a[i]).count('1')
    y = n-i-1-(res[n-1]-res[i])
    ans = min(ans,x+y)
print(ans)

### FIXED CODE ###

import numpy
n = int(input())
s = input()
a = [0]*n
ans = 10**6
for i in range(n):
    if s[i] == 'W':
        a[i] = 1
res = numpy.cumsum(a)
for i in range(n):
    x = res[i]-str(a[i]).count('1')
    y = n-i-1-(res[n-1]-res[i])
    ans = min(ans,x+y)
print(ans)
a,b=input().split("\n")
print(len(b.split("ABC"))-1)

### FIXED CODE ###

a = input()
a = input()
print(a.count("ABC"))
def f1(a,b,s):
	print(s[a:b+1])
	return s

def f2(a,b,s):
	temp = s[a:b+1]
	return s[:a] + temp[::-1] + s[b+1:]

def f3(a,b,p,s):
	return s[:a] + p + s[b+1:]
'''
data = input()
q = int(input())
'''
data = 'abcdefg'
q = 1
functions = {'print':f1, 'reverse':f2, 'replace':f3}

for i in range(q):
	temp = [s if s.isalpha() else int(s) for s in input().split(' ')]
	f = temp.pop(0)
	data = functions[f](*temp,data)

### FIXED CODE ###

def f1(s,a,b):
	print(s[a:b+1])
	return s

def f2(s,a,b):
	temp = s[a:b+1]
	return s[:a] + temp[::-1] + s[b+1:]

def f3(s,a,b,p):
	return s[:a] + p + s[b+1:]

data = input()
q = int(input())
functions = {'print':f1, 'reverse':f2, 'replace':f3}

for i in range(q):
	temp = [s if s.isalpha() else int(s) for s in input().split(' ')]
	f = temp.pop(0)
	data = functions[f](data,*temp)