Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
S = list(input())
N = len(S)

cnt = 0
l = 0
r = N-1
while l<=r:
    if S[l]=="x" and S[r]!="x":
        l += 1
        cnt += 1
    if S[l]!="x" and S[r]=="x":
        r += -1
        cnt += 1
    if S[l]=="x" and S[r]=="x":
        l += 1
        r += -1
    if S[l]!="x" and S[r]!="x":
        if S[l]==S[r]:
            l += 1
            r += -1
        else:
            cnt = -1
            break
print(cnt)

Fixed submission:
S = list(input())
N = len(S)

cnt = 0
l = 0
r = N-1
while l<=r:
    if S[l]=="x" and S[r]!="x":
        l += 1
        cnt += 1
    elif S[l]!="x" and S[r]=="x":
        r += -1
        cnt += 1
    elif S[l]=="x" and S[r]=="x":
        l += 1
        r += -1
    elif S[l]!="x" and S[r]!="x":
        if S[l]==S[r]:
            l += 1
            r += -1
        else:
            cnt = -1
            break
print(cnt)
Buggy python submission (verdict: Runtime Error):
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 = list(map(int,input().split()))

AB = []
for i in range(M):
    AB.append(list(map(int,input().split())))

out = 0
for break in range(M):
    Network = UnionFind(N)
    for j in range(M):
        if j!=break:
            A,B=AB[j][0],AB[j][1]
            Network.union(A-1, B-1)
    if Network.group_count()==2:
        out+=1
print(out)

Fixed submission:
N,M = list(map(int,input().split()))
AB = []
from collections import deque

for i in range(M):
    AB.append(list(map(int,input().split())))


out = 0
for remove in range(M):
    G = [[] for i in range(N)]
    for i in range(M):
        if i==remove:
            continue
        else:
            A,B=AB[i]
            G[A-1].append(B-1)
            G[B-1].append(A-1)
    s = 0
    visit = [0]*N
    que = deque([s])
    visit[s] = 1
    while que:
        v = que.popleft()
        for w in G[v]:
            if visit[w] == 1:
                continue
            visit[w] =  1
            que.append(w)
    if sum(visit)!=N:
        out+=1
print(out)
Buggy python submission (verdict: Runtime Error):
H,W = list(map(int,input().split()))
N = int(input())
A = list(map(int,input().split()))
cnt = 0
L = []
i = 1
while cnt<H*W:
    if A[i-1]>0:
        A[i-1]+=-1
        L.append(i)
        cnt+=1
    else:
        i+=1
for i in range(H):
    out = [0]*W
    if i%2==0:
        for j in range(W):
            out[j]=L[H*i+j]
    else:
        for j in range(W):
            out[W-1-j]=L[H*i+j]
    print(*out)

Fixed submission:
H,W = list(map(int,input().split()))
N = int(input())
A = list(map(int,input().split()))
cnt = 0
L = []
i = 1
while cnt<H*W:
    if A[i-1]>0:
        A[i-1]+=-1
        L.append(i)
        cnt+=1
    else:
        i+=1
for i in range(H):
    out = [0]*W
    if i%2==0:
        for j in range(W):
            out[j]=L[W*i+j]
    else:
        for j in range(W):
            out[W-1-j]=L[W*i+j]
    print(*out)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
a.sort()
flag = False
b = [False] * 1000010
for i in range(n - 1):
    if a[i] == a[i + 1]:
        for j in range(a[i], 1000010, a[i]):
            b[j] = True

ans = 0
for i in range(n):
    if not b[a[i]]:
        ans += 1
        for j in range(a[i], 1000010, a[i]):
            b[j] = True

print(ans)


Fixed submission:
n = int(input())
a = list(map(int, input().split()))
a.sort()
flag = False
b = [False] * 1000010
for i in range(n - 1):
    if a[i] == a[i + 1]:
        if b[a[i]]:
            continue
        for j in range(a[i], 1000010, a[i]):
            b[j] = True

ans = 0
for i in range(n):
    if not b[a[i]]:
        ans += 1
        for j in range(a[i], 1000010, a[i]):
            b[j] = True

print(ans)
Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
high = list(map(int, input().split()))

judge = ["ok" for i in range(n)]

for i in range(m):
    a, b = [int(s) - 1 for i in input().split()]

    if high[a] < high[b]:
        judge[a] = "ng"
    elif high[a] > high[b]:
        judge[b] = "ng"
    else:
        judge[a] = "ng"
        judge[b] = "ng"

print(judge.count("ok"))


Fixed submission:
n, m = map(int, input().split())
high = list(map(int, input().split()))

judge = ["ok" for i in range(n)]

for i in range(m):
    a, b = [int(s) - 1 for s in input().split()]

    if high[a] < high[b]:
        judge[a] = "ng"
    elif high[a] > high[b]:
        judge[b] = "ng"
    else:
        judge[a] = "ng"
        judge[b] = "ng"

print(judge.count("ok"))
Buggy python submission (verdict: Runtime Error):
x = int(input())

b = 0
while True:
    if int((x + b ** 5) ** 0.2) == (x + b ** 5) ** 0.2:
        a = int((x + b ** 5) ** 0.2)
        break
    elif int((x - b ** 5) ** 0.2) == (x - b ** 5) ** 0.2:
        a = int((x - b ** 5) ** 0.2)
        b *= -1
        break
    else:
        b += 1

print(str(a) + " " + str(b))


Fixed submission:
x = int(input())

flag = False
for a in range(-200, 200):
    for b in range(-200, 200):
        if a ** 5 - b ** 5 == x:
            flag = True
            print(str(a) + " " + str(b))
            break
    if flag:
        break
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))

# i < j
# j - i = a[i] + a[j]
# a[i] + i = j - a[j]

count = 0
candidate = []
for i in range(n):
    sa = i - a[i]
    count += candidate.count(sa)
    wa = i + a[i]
    candidate.append(wa)

print(count)


Fixed submission:
from collections import defaultdict

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

d = defaultdict(int)
ans = 0
for i in range(n):
    ans += d[i - a[i]]
    d[a[i] + i] += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))

# i < j
# j - i = a[i] + a[j]
# a[i] + i = j - a[j]

count = 0
for i in range(n):
    
    b_i = a[i] + i
    
    for j in range(i + 1, n):
        if b_i == j - a[j]:
            count += 1

print(count)


Fixed submission:
from collections import defaultdict

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

d = defaultdict(int)
ans = 0
for i in range(n):
    ans += d[i - a[i]]
    d[a[i] + i] += 1

print(ans)
Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())

if n % 2 == 0:
    i = 1
    while i <= m:
        if i < (n - 2) // 2
            a_i = str(i)
        else:
            a_i = str(i + 1)
        b_i = str(n - i)
        
        print(a_i + " " + b_i)
        i += 1
        
elif n % 2 == 1:
    for i in range(1, m + 1):
        a_i = str(i)
        b_i = str(n - i)
        
        print(a_i + " " + b_i)


Fixed submission:
n, m = map(int, input().split())

if n % 2 == 0:
    i = 1
    while i <= m:
        a_i = str(i)
        
        if i <= (n - 2) // 4:
            b_i = str(n - i)
        else:
            b_i = str(n - i - 1)
        
        print(a_i + " " + b_i)
        i += 1
        
elif n % 2 == 1:
    for i in range(1, m + 1):
        a_i = str(i)
        b_i = str(n - i)
        
        print(a_i + " " + b_i)

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())

if n % 2 == 0:
    i = 1
    while i <= m:
        if i < (n - 2) // 2
            a_i = str(i)
        else:
            a_i = str(i + 1)
        b_i = str(n - i)
        
        print(a_i)
        i += 1
        
elif n % 2 == 1:
    for i in range(1, m + 1):
        a_i = str(i)
        b_i = str(n - i)
        
        print(a_i + " " + b_i)


Fixed submission:
n, m = map(int, input().split())

if n % 2 == 0:
    i = 1
    while i <= m:
        a_i = str(i)
        
        if i <= (n - 2) // 4:
            b_i = str(n - i)
        else:
            b_i = str(n - i - 1)
        
        print(a_i + " " + b_i)
        i += 1
        
elif n % 2 == 1:
    for i in range(1, m + 1):
        a_i = str(i)
        b_i = str(n - i)
        
        print(a_i + " " + b_i)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

line = list(map(int, input().strip().split(" ")))

for i in range(1, n + 1):
    print(line.count(i))


Fixed submission:
n = int(input())

line = list(map(int, input().strip().split(" ")))

ans = [0 for s in range(n)]

for i in line:
    ans[i - 1] += 1

for i in range(n):
    print(ans[i])
Buggy python submission (verdict: Time Limit Exceeded):
import math
n = int(input())
a = list(map(int, input().split()))

def lcm(a,b):
    return a * b // math.gcd(a,b)

p = 1000000007

k = 1
for i in range(n):
    k = lcm(k,a[i])

ans = 0
for i in range(n):
    ans += (k // a[i]) % p
    ans %= p

print(ans)


Fixed submission:
import math
n = int(input())
a = list(map(int, input().split()))

def lcm(a,b):
    return a * b // math.gcd(a,b)

p = 1000000007

k = 1
for i in range(n):
    k = lcm(k,a[i])

k %= p

ans = 0
for i in range(n):
    ans += k * pow(a[i],p-2,p)
    ans %= p

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import itertools
n, m = map(int, input().split())
a = [int(input()) for i in range(m)]
mod = 10 ** 9 + 7
dp = [0 for i in range(n + 1)]

dp[0] = 1

for i in range(1, n + 1):
    if i in a:
        continue
    else:
        dp[i] += dp[i - 1]
        dp[i] %= mod
        if i != 1:
            dp[i] += dp[i - 2]
            dp[i] %= mod

print(dp[n])


Fixed submission:
import itertools
n, m = map(int, input().split())
a = set([int(input()) for i in range(m)])
mod = 10 ** 9 + 7
dp = [0] * (n + 1)

dp[0] = 1
dp[1] = 0 if 1 in a else 1

for i in range(2, n + 1):
    if i in a:
        continue
    else:
        dp[i] = (dp[i - 1] + dp[i - 2]) % mod

print(dp[n])
Buggy python submission (verdict: Runtime Error):
n = int(input())

G = {}
for i in range(n):
    G.update({i: []})

for i in range(n - 1):
    u, v, w = map(int, input().split())
    G[u - 1].append([v - 1, w % 2])
    G[v - 1].append([u - 1, w % 2])

color = [-1 for i in range(n)]

def dfs(graph, v, c):
    color[v] = c
    
    for next_v in graph[v]:
        dis = next_v[1]
        if color[next_v[0]] != -1:
            continue
        
        if dis == 0:
            n_c = c
        elif dis == 1:
            n_c = 1 - c
        
        dfs(graph, next_v[0], n_c)

dfs(G, 0, 0)
[print(i) for i in color]


Fixed submission:
import sys
sys.setrecursionlimit(200000)

n = int(input())
 
G = {}
for i in range(n):
    G.update({i: []})
 
for i in range(n - 1):
    u, v, w = map(int, input().split())
    G[u - 1].append([v - 1, w % 2])
    G[v - 1].append([u - 1, w % 2])
 
color = [-1 for i in range(n)]
 
def dfs(graph, v, c):
    color[v] = c
    
    for next_v in graph[v]:
        dis = next_v[1]
        if color[next_v[0]] != -1:
            continue
        
        if dis == 0:
            n_c = c
        elif dis == 1:
            n_c = 1 - c
        
        dfs(graph, next_v[0], n_c)
 
dfs(G, 0, 0)
[print(i) for i in color]
Buggy python submission (verdict: Runtime Error):
def main():
    n,w = map(int, input().split())
    weight = []
    value = []
    for i in range(n):
        wi, vi = map(int, input().split())
        weight.append(wi)
        value.append(vi)
    
    def chmax(a, b):
        if a < b:
            a = b
        return a
    
    dp = [[0 for i in range(110)] for j in range(100100)]

    for i in range(n):
        for sum_w in range(w + 1):
            if sum_w - weight[i] >= 0:
                dp[i + 1][sum_w] = chmax(dp[i + 1][sum_w], dp[i][sum_w - weight[i]] + value[i])

            dp[i + 1][sum_w] = chmax(dp[i + 1][sum_w], dp[i][sum_w])

    print(dp[n][w])

if __name__ == "__main__":
    main()


Fixed submission:
def main():
    n,w = map(int, input().split())
    weight = []
    value = []
    for i in range(n):
        wi, vi = map(int, input().split())
        weight.append(wi)
        value.append(vi)
    
    def chmax(a, b):
        if a < b:
            a = b
        return a
    
    dp = [[0 for i in range(100100)] for j in range(110)]

    for i in range(n):
        for sum_w in range(w + 1):
            if sum_w - weight[i] >= 0:
                dp[i + 1][sum_w] = chmax(dp[i + 1][sum_w], dp[i][sum_w - weight[i]] + value[i])

            dp[i + 1][sum_w] = chmax(dp[i + 1][sum_w], dp[i][sum_w])
    
    print(dp[n][w])

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
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 same(self, x, y):
    return self.find(x) == self.find(y)

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

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

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

  def num_members(self,x):
    return abs(self.parents[self.find(x)])

  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)
cnt = 0
for i in range(M):
  a,b = map(int, input().split())
  if not uf.same(a,b):
    cnt += 1
    uf.union(a,b)
print(N-1-cnt)


Fixed submission:
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 same(self, x, y):
    return self.find(x) == self.find(y)

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

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

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

  def num_members(self,x):
    return abs(self.parents[self.find(x)])

  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)
cnt = 0
for i in range(M):
  a,b = map(int, input().split())
  if not uf.same(a-1,b-1):
    cnt += 1
    uf.union(a-1,b-1)
print(N-1-cnt)
Buggy python submission (verdict: Time Limit Exceeded):
#素因数分解、計算量はO(√N)
from collections import defaultdict
def factorize(n):
  b = 2
  dic = defaultdict(lambda: 0)
  while b * b <= n:
    while n % b == 0:
      n //= b
      dic[b] += 1
    b = b + 1
  if n > 1:
    dic[n] += 1
  return dic
N = int(input())
fct = factorize(2*N)
lis = []
for k,v in fct.items():
  lis.append(pow(k,v))
ans = 10**18
from itertools import groupby, accumulate, product, permutations, combinations
for pro in product([0,1],repeat=len(lis)):
  prod1 = 1
  for i,p in enumerate(pro):
    if p==1:
      prod1 *= lis[i]
  prod2 = (N*2)//prod1
  if prod1<prod2:
    prod1,prod2 = prod2,prod1
    # continue
  for i in range(1,prod2+1):
    # if prod1*i>ans:
      # break
    if (prod1*i-1)%prod2==0:
      ans = min(ans, prod1*i-1)
      break
    if (prod1*i+1)%prod2==0:
      ans = min(ans, prod1*i)
      break
print(ans)



Fixed submission:
#素因数分解、計算量はO(√N)
from collections import defaultdict
def factorize(n):
  b = 2
  dic = defaultdict(lambda: 0)
  while b * b <= n:
    while n % b == 0:
      n //= b
      dic[b] += 1
    b = b + 1
  if n > 1:
    dic[n] += 1
  return dic
N = int(input())
fct = factorize(2*N)
lis = []
for k,v in fct.items():
  lis.append(pow(k,v))
ans = 10**18
from itertools import groupby, accumulate, product, permutations, combinations
for pro in product([0,1],repeat=len(lis)):
  if sum(pro)==0:
    continue
  prod1 = 1
  for i,p in enumerate(pro):
    if p==1:
      prod1 *= lis[i]
  prod2 = (N*2)//prod1
  if prod1<prod2:
    prod1,prod2 = prod2,prod1
  
  for i in range(1,prod2+1):
    if prod1*i>ans:
      break
    if (prod1*i-1)%prod2==0:
      ans = min(ans, prod1*i-1)
      break
    if (prod1*i+1)%prod2==0:
      ans = min(ans, prod1*i)
      break

print(ans)
Buggy python submission (verdict: Runtime Error):
# Binary Indexed Tree (Fenwick Tree)
class BIT:
  def __init__(self, n):
    self.n = n
    self.bit = [0]*(n+1)
    self.el = [0]*(n+1)
  def sum(self, i):
    s = 0
    while i > 0:
      s += self.bit[i]
      i -= i & -i
    return s
  def add(self, i, x):
    # assert i > 0
    self.el[i] += x
    while i <= self.n:
      self.bit[i] += x
      i += i & -i
  def get(self, i, j=None):
    if j is None:
      return self.el[i]
    return self.sum(j) - self.sum(i-1)

mod = 998244353
N, K = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(K)]
A.sort()
dp = BIT(N)
dp.add(1,1)
for i in range(2,N+1):
  for l,r in A:
    if i-l<=0:# Binary Indexed Tree (Fenwick Tree)
class BIT:
  def __init__(self, n):
    self.n = n
    self.bit = [0]*(n+1)
    self.el = [0]*(n+1)
  def sum(self, i):
    s = 0
    while i > 0:
      s += self.bit[i]
      i -= i & -i
    return s
  def add(self, i, x):
    # assert i > 0
    self.el[i] += x
    while i <= self.n:
      self.bit[i] += x
      i += i & -i
  def get(self, i, j=None):
    if j is None:
      return self.el[i]
    return self.sum(j) - self.sum(i-1)

mod = 998244353
N, K = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(K)]
A.sort()
dp = BIT(N)
dp.add(1,1)
for i in range(2,N+1):
  for l,r in A:
    if i-l<=0:
      continue
    if i-r>0:
      dp.add(i,dp.get(i-r,i-l)%mod)
    else:
      dp.add(i,dp.sum(i-l)%mod)
print(dp.el[-1]%mod)


      continue
    if i-r>0:
      dp.add(i,dp.get(i-r,i-l)%mod)
    else:
      dp.add(i,dp.sum(i-l)%mod)
print(dp.el[-1]%mod)



Fixed submission:
# Binary Indexed Tree (Fenwick Tree)
class BIT:
  def __init__(self, n):
    self.n = n
    self.bit = [0]*(n+1)
    self.el = [0]*(n+1)
  def sum(self, i):
    s = 0
    while i > 0:
      s += self.bit[i]
      i -= i & -i
    return s
  def add(self, i, x):
    # assert i > 0
    self.el[i] += x
    while i <= self.n:
      self.bit[i] += x
      i += i & -i
  def get(self, i, j=None):
    if j is None:
      return self.el[i]
    return self.sum(j) - self.sum(i-1)

mod = 998244353
N, K = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(K)]
A.sort()
dp = BIT(N)
dp.add(1,1)
for i in range(2,N+1):
  for l,r in A:
    if i-l<=0:
      continue
    if i-r>0:
      dp.add(i,dp.get(i-r,i-l)%mod)
    else:
      dp.add(i,dp.sum(i-l)%mod)
print(dp.el[-1]%mod)
Buggy python submission (verdict: Runtime Error):
N, X, M = map(int, input().split())
lis = [0]*M
lis[0] = X
check = [-1]*M
check[X] = 0
for i in range(1,M):
  lis[i] = pow(lis[i-1],2,M)
  if check[lis[i]]>=0:
    cyc = i-check[lis[i]]
    start = check[lis[i]]
    break
  check[lis[i]] = i
from itertools import groupby, accumulate, product, permutations, combinations
cum = list(accumulate(lis))
cyc_cum = cum[-1]-cum[start]
if N-1<=start+cyc:
  ans = cum[N-1]
else:
  ans = cyc_cum*((N-1-start)//cyc)+cum[(N-1-start)%cyc+start]
print(ans)

Fixed submission:
N, X, M = map(int, input().split())
if M==1:
  print(0)
  exit()
lis = [0]*M
lis[0] = X
check = [-1]*M
check[X] = 0
for i in range(1,M):
  lis[i] = pow(lis[i-1],2,M)
  if check[lis[i]]>=0:
    cyc = i-check[lis[i]]
    start = check[lis[i]]
    break
  check[lis[i]] = i
from itertools import groupby, accumulate, product, permutations, combinations
cum = list(accumulate(lis))
cyc_cum = cum[-1]-cum[start]
if N-1<=start+cyc:
  ans = cum[N-1]
else:
  ans = cyc_cum*((N-1-start)//cyc)+cum[(N-1-start)%cyc+start]
print(ans)

Buggy python submission (verdict: Runtime Error):
N, X, M = map(int, input().split())
lis = [0]*M
lis[0] = X
check = [-1]*M
check[X] = 0
for i in range(1,M):
  lis[i] = pow(lis[i-1],2,M)
  if check[lis[i]]>=0:
    cyc = i-check[lis[i]]
    start = check[lis[i]]
    break
  check[lis[i]] = i
from itertools import groupby, accumulate, product, permutations, combinations
cum = list(accumulate(lis))
cyc_cum = cum[-1]-cum[start]
if N<=start:
  ans = cum[N-1]
else:
  ans = cyc_cum*((N-start)//cyc)+cum[(N-start)%cyc+start-1]
print(ans)

Fixed submission:
N, X, M = map(int, input().split())
if M==1:
  print(0)
  exit()
lis = [0]*M
lis[0] = X
check = [-1]*M
check[X] = 0
for i in range(1,M):
  lis[i] = pow(lis[i-1],2,M)
  if check[lis[i]]>=0:
    cyc = i-check[lis[i]]
    start = check[lis[i]]
    break
  check[lis[i]] = i
from itertools import groupby, accumulate, product, permutations, combinations
cum = list(accumulate(lis))
cyc_cum = cum[-1]-cum[start]
if N-1<=start+cyc:
  ans = cum[N-1]
else:
  ans = cyc_cum*((N-1-start)//cyc)+cum[(N-1-start)%cyc+start]
print(ans)