Harryxun/stf_run huggingface dataset:
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
AM = A.pop(0)
xy = []
if AM > 0 and A[-1] >= 0:
    p = 1
elif AM <= 0 and A[-1] < 0:
    p = 2
else:
    p = 3
if p == 1:
    while(len(A)>1):
        x = A.pop(-1)
        y = A.pop(0)
        xy.append([x, y])
        A.append(x-y)
    xy.append([AM, A[0]])
    ans = AM - A[0]
if p == 2:
    while(len(A)>1):
        x = AM
        y = A.pop(-1)
        xy.append([x, y])
        AM -= y
    xy.append([AM, A[0]])
    ans = AM - A[0]
if p == 3:
    while(len(A)>1):
        y = A.pop(0)
        if y >= 0:
            x = A.pop(-1)
            xy.append([x, y])
            xy.append([x, y])
            A.append(x-y)
        else:
            x = AM
            xy.append([x, y])
            AM -= y
            xy.append([AM, A[0]])
            ans = AM - A[0]
print(ans)
for out in xy:
    print(*out)

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
AM = A.pop(0)
xy = []
if AM > 0 and A[-1] >= 0:
    p = 1
elif AM <= 0 and A[-1] < 0:
    p = 2
else:
    p = 3
if p == 1:
    while(len(A)>1):
        x = A.pop(-1)
        y = A.pop(0)
        xy.append([x, y])
        A.append(x-y)
    xy.append([AM, A[0]])
    ans = AM - A[0]
if p == 2:
    while(len(A)>1):
        x = AM
        y = A.pop(-1)
        xy.append([x, y])
        AM -= y
    xy.append([AM, A[0]])
    ans = AM - A[0]
if p == 3:
    while(len(A)>1):
        y = A.pop(0)
        if y >= 0:
            x = A.pop(-1)
            xy.append([x, y])
            A.append(x-y)
        else:
            x = AM
            xy.append([x, y])
            AM -= y
    xy.append([AM, A[0]])
    ans = AM - A[0]
print(ans)
for out in xy:
    print(*out)
import math
H, A = input().split()
print(math.ceil(H / A))

### FIXED CODE ###

import math
H, A = map(int, input().split(' '))
print(math.ceil(H / A))
n,k = map(int, input().split())
a =list(map(int, input().split()))
ans =1

if n == k:
   for i in a:
       ans *= i
       if abs(ans) >10**9+7:
           ans = ans%(10**9+7)

elif all(i <= 0 for i in a) and k%2 ==1:
    a.sort(reverse = True)
    for i in range(k):
        ans *= a[i]
        if abs(ans) > 10 ** 9 + 7:
            ans = ans % (10 ** 9 + 7)

else:
    am =[]
    ap =[]

    a.sort(key = abs,reverse = True)
    amax = a[:k]
    amin = a[k:]
    c=1
    if 0 in amax:
        ans = 0
    else:
        for i in amax:
            if i<0:
                am.append(i)
                c *=-1
            elif i>0:
                ap.append(i)
        if c ==1:
            for i in amax:
                ans *= i
                if abs(ans) > 10 ** 9 + 7:
                    ans = ans % (10 ** 9 + 7)
        else:

            if min(amin)/min(ap) > max(amin)/max(am):
                amax.remove(max(am))
                amax.append(max(amin))
            else:
                amax.remove(min(ap))
                amax.append(min(amin))
            for i in amax:
                ans *= i
                if abs(ans) > 10 ** 9 + 7:
                    ans = ans % (10 ** 9 + 7)

print(ans% (10 ** 9 + 7))


### FIXED CODE ###

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

if n == k:
   for i in a:
       ans *= i
       if abs(ans) >10**9+7:
           ans = ans%(10**9+7)

elif all(i <= 0 for i in a) and k%2 ==1:
    a.sort(reverse = True)
    for i in range(k):
        ans *= a[i]
        if abs(ans) > 10 ** 9 + 7:
            ans = ans % (10 ** 9 + 7)

else:
    am =[]
    ap =[]

    a.sort(key = abs,reverse = True)
    amax = a[:k]
    amin = a[k:]
    c=1
    if 0 in amax:
        ans = 0
    else:
        for i in amax:
            if i<0:
                am.append(i)
                c *=-1
            elif i>0:
                ap.append(i)
        if c ==1:
            for i in amax:
                ans *= i
                if abs(ans) > 10 ** 9 + 7:
                    ans = ans % (10 ** 9 + 7)
        else:
            if ap == []:
                amax.remove(max(am))
                amax.append(max(amin))

            elif max(am)*min(amin) < min(ap)*max(amin):
                amax.remove(max(am))
                amax.append(max(amin))
            else:
                amax.remove(min(ap))
                amax.append(min(amin))
            for i in amax:
                ans *= i
                if abs(ans) > 10 ** 9 + 7:
                    ans = ans % (10 ** 9 + 7)

print(ans % (10 ** 9 + 7))
# -*- coding: utf-8 -*-

a,b,c,d = map(int,input().split())

num = 0

import math

lcm = c*d//int(math.gcd(c, d))

num = b-b//c-b//d+b//lcm-((a-1)-(a-1)//c-(a-1)//d+(a-1)//lcm)





print(str(num))

### FIXED CODE ###

# -*- coding: utf-8 -*-

a,b,c,d = map(int,input().split())

num = 0

import fractions
lcm = c*d//int(fractions.gcd(c, d))

num = b-b//c-b//d+b//lcm-((a-1)-(a-1)//c-(a-1)//d+(a-1)//lcm)





print(str(num))
N = int(input())
c = list(input())
counter = 0
left = 0
right = N-1

while left < right:
    if c[left] == 'W':
        flag = True
        while flag:
            if c[right] == 'R':
                c[right] = 'W'
                c[left] = 'R'
                counter += 1
                right -= 1
                left += 1
                flag = False
            else:
                right -= 1
    else:
        left += 1

print(counter)

### FIXED CODE ###

N = int(input())
c = list(input())
counter = 0
left = 0
right = N-1

while left < right:
    if c[left] == 'W':
        flag = True
        while flag:
            if left >= right:
                flag = False
            if c[right] == 'R':
                c[right] = 'W'
                c[left] = 'R'
                counter += 1
                right -= 1
                flag = False
            else:
                right -= 1
    left += 1

print(counter)
import numpy as np
from numpy.linalg import solve

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

right = np.asarray(a)*2
left = np.zeros([n,n])
for i in range(n):
    if i != n-1:
        left[i,i:i+2] = 1
    else:
        left[i,0] = 1
        left[i,i] = 1
        
out = list(map(int, solve(left, right)))
print(" ".join(map(str, out)))

### FIXED CODE ###

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

s = sum(a)
b = [a[i] for i in range(n) if i % 2 == 1]
x1 = s - 2 * sum(b)
x = []
x.append(x1)
xi = x1
for i in range(0, n-1, 1):
    #print(a[i])
    xi_1 = 2 * a[i] - xi
    x.append(xi_1)
    #print(xi_1)
    xi = xi_1
    
print(" ".join(map(str, x)))
if input()='SUN':
	print(7)
elif input()='MON':
  print(6)
elif input()='TUE':
  print(5)
elif input()='WED':
  print(4)
elif input()='THU
  print(3)
elif input()='FRI':
  print(2)
elif input()='SAT':
  print(1)


### FIXED CODE ###

s=input()
if s=='MON':
  print(6)
elif s=='TUE':
  print(5)
elif s=='WED':
  print(4)
elif s=='THU':
  print(3)
elif s=='FRI':
  print(2)
elif s=='SAT':
  print(1)
else:
  print(7)
#encoding=utf-8
import sys

def inp():
	for i in sys.stdin:
		xa,ya,xb,yb,xc,yc,xd,yd = map(float, i.split(","))	
		print hantei(xa,ya,xb,yb,xc,yc,xd,yd)

def hantei(xa,ya,xb,yb,xc,yc,xd,yd):
	count = [0,0,0,0]
	a1 = (ya - yc)/(xa - yc)
	b1 = ya - a1*xa

	a2 = (yb - yd)/(xb - xd)
	b2 = yb - a2*xb

	if xb*a1+b1 > yb:
		count[0] += 1
	else:
		count[1] += 1
	if xd*a1+b1 > yd:
		count[0] += 1
	else:
		count[1] += 1
	if xa*a2+b2 > ya:
		count[2] += 1
	else:
		count[3] += 1
	if xc*a2+b2 > yc:
		count[2] += 1
	else:
		count[3] += 1

	for i in xrange(4):
		if count[i] != 1:
			return "NO"
		else:
			pass
	return "YES"

if __name__ == "__main__":
	inp()

### FIXED CODE ###

def S(x1,y1,x2,y2,x3,y3):
    return ((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1))/2.0
import sys
for dataset in sys.stdin:
    xa,ya,xb,yb,xc,yc,xd,yd=map(float,dataset.split(","))
    ABC=S(xa,ya,xb,yb,xc,yc)
    BCD=S(xb,yb,xc,yc,xd,yd)
    CDA=S(xc,yc,xd,yd,xa,ya)
    DAB=S(xd,yd,xa,ya,xb,yb)
    if ABC>0 and BCD>0 and CDA>0 and DAB>0:
        print "YES"
    elif ABC<0 and BCD<0 and CDA<0 and DAB<0:
        print "YES"
    else:
        print "NO"
word = input()

flag = True
if len(word)%2 == 1:
    flag = False

wordset = set(word)
leng = len(wordset)
i = 0
while flag and (i<leng):
    if (word.count(wordset[i]))%2 == 1:
        flag = False
    i += 1
if flag:print("Yes")
else: print("No")


### FIXED CODE ###

word = input()

flag = True
if len(word)%2 == 1:
    flag = False

wordset = set(word)
leng = len(wordset)
for s in wordset:
    if word.count(s)%2 == 1:
        flag = False
        break
if flag:print("Yes")
else: print("No")
a=int(input())
b=list(map(int,input().split))
 
s=0
for i in range(a):
  for j in range(i+1,a):
    s+=b[i]*b[j]
print(s)

### FIXED CODE ###

a=int(input())
b=list(map(int,input().split()))
 
s=0
for i in range(a):
  for j in range(i+1,a):
    s+=b[i]*b[j]
print(s)
import sys
input = sys.stdin.readline
n = int(input())
s = input()
ans = 0
for i in range(n - 1):
  idx = n - 1 - i
  if s[idx] == 'W' and s[idx + 1] == 'R':
    ans += 1
    s[idx] = 'R'
print(ans)


### FIXED CODE ###

import sys
input = sys.stdin.readline
n = int(input())
s = input()
ans = 0
r = [0] * n
w = [0] * n
for i in range(n):
  if s[i] == 'W':
    w[i] += 1
  else:
    r[i] += 1
for i in range(1, n):
  w[i] += w[i - 1]
  r[i] += r[i - 1]
rn = r[n - 1]
ans = n
if rn == 0:
  ans = 0
for i in range(n):
  c = (i + 1)
  ri = r[i]
  left = rn - ri
  need = c - ri
  ans = min(ans, max(left, need))  

print(ans)
l = list(map(int, input().split()))
     
    if l[0]<2 or l[0]>20 or l[1]<1 or l[1]>20:
    	sys.exit()
     
    a = 0
    while l[0] <= l[1]:
    	l[1] = l[1] - l[0]
    	a += 1
    print(a)

### FIXED CODE ###

S=input()
T=input()
if len(S)>3 or len(S) < 1 or len(T)>3 or len(T) < 1:
  sys.exit()

a = 0
if S[0] == T[0]:
  a += 1
if S[1] == T[1]:
  a += 1
if S[2] == T[2]:
  a += 1

print(a)
lst=input().split()
x_1=lst[0]
y_1=int(lst[1])
x_2=int(lst[2])
y_2=int(lst[3])
dis=((x_1-y_1)**2+(x_2-y_2)**2)**(1/2)
print(dis)


### FIXED CODE ###

lst=input().split()
lst=list(map(lambda i:float(i),lst))
x_1=lst[0]
x_2=lst[1]
y_1=lst[2]
y_2=lst[3]
dis=((x_1-y_1)**2+(x_2-y_2)**2)**(1/2)
print(dis)
n, k = map(int, input().split())
s = input()
pre = s[0]
rsans = s[0]
first = s[0]

if s[0] == "R":
    second = "L"
else:
    second = "R"

for i in s[1:]:
    if i == second and k > 0:
        ans = ans + first
        pre = i
    elif i == first and pre == second:
        k -= 1
        ans = ans + i
        pre = i
    elif k == 0:
        ans = ans + i
    elif i == first and pre == first:
        ans = ans + i

pre = "0"
cnt = 0

for i in ans:
    if i == pre:
        cnt += 1
    pre = i

print(cnt)

### FIXED CODE ###

n, k = map(int, input().split())
s = input()
pre = s[0]
ans = s[0]
first = s[0]

if s[0] == "R":
    second = "L"
else:
    second = "R"

for i in s[1:]:
    if i == second and k > 0:
        ans = ans + first
        pre = i
    elif i == first and pre == second:
        k -= 1
        ans = ans + i
        pre = i
    elif k == 0:
        ans = ans + i
    elif i == first and pre == first:
        ans = ans + i

pre = "0"
cnt = 0

for i in ans:
    if i == pre:
        cnt += 1
    pre = i

print(cnt)
A,B = map(int,input().split())
if 1 <= A and A <=9 and 1 <= B and B <=9 :
    print("Yes")
else:
    print("No")

### FIXED CODE ###

N = int(input())
a = 0
for i in range(1,10):
    if N % i == 0:
        if N/i <= 9:
            a = 1
        else:
            continue
    else:
        continue
if a == 1:
    print("Yes")
else:
    print("No")
A = int(input())
B = int(input())
print(A*B)

### FIXED CODE ###

A, B = map(int, input().split())
print(A * B)
def func():
    N = int(input())
    serial = list(range(1, N + 1))

    print(sum(serial)-N)


if __name__ == '__main__':
    func()


### FIXED CODE ###

def func():
    N = int(input())
    sum = 0
    for i in range(1, N + 1):
        sum += i

    print(sum-N)


if __name__ == '__main__':
    func()
n = int(input())
s = input()
k = int(input())
sa = s[k: k + 1]
sl = len(s)
sli = list(s)
hey = []
for sli in sli:
    if sli == sa:
        hey.append(sli)
    else:
        hey.append("*")
hey = "".join(hey)
print(hey)

### FIXED CODE ###

a, b, c = map(int, input().split())
if b > a:
    if b > c and a < c:
        print("Yes")
    else:
        print("No")
elif a > b:
    if a > c and b < c:
        print("Yes")
    else:
        print("No")
else:
    print("No")
n=int(input())
aa=list(map(int,input().split()))
a=[x for x in aa if x!=1]
n=len(a)
# 素因数分解
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0:
        cnt+=1
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr
tmp=[i for i,j in factorization(min(a))]
not_flg=False
for t in tmp:
  flg=True
  for x in a:
    if x%t==0:
      pass
    else:
      flg=False
      break
  if flg:
    not_flg=flg
    break
if not_flg:
  print('not coprime')
  exit()
if n>78498:
  print('setwise coprime')
  exit()
ary=set(())
for x in a:
  tmp={i for i,j in factorization(x)}
  for t in tmp:
    if t in ary:
      print('setwise coprime')
      exit()
    else:
      ary.add(t)
print('pairwise coprime')


### FIXED CODE ###

nn=int(input())
aa=list(map(int,input().split()))
a=[x for x in aa if x!=1]
n=len(a)
if n<=1:
  print('pairwise coprime')
  exit()

# 素因数分解
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0:
        cnt+=1
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr
tmp=[i for i,j in factorization(min(a))]
not_flg=False
for t in tmp:
  flg=True
  for x in a:
    if x%t==0:
      pass
    else:
      flg=False
      break
  if flg:
    not_flg=flg
    break
if not_flg:
  if n==nn:
    print('not coprime')
  else:
    print('setwise coprime')
  exit()
#if n>78499:
if n>99999:
  print('setwise coprime')
  exit()
ary=set(())
for x in a:
  tmp={i for i,j in factorization(x)}
  for t in tmp:
    if t in ary:
      print('setwise coprime')
      exit()
    else:
      ary.add(t)
print('pairwise coprime')
H, W = map( int, input().split())
d = dict()
S = [["#"]*(W+2)]+[ "#"+input()+"#" for _ in range(H)]+ [["#"]*(W+2)]
H += 2
W += 2
L = [[0]*(W+2) for _ in range(H+2)]
R = [[0]*(W+2) for _ in range(H+2)]
D = [[0]*(W+2) for _ in range(H+2)]
U = [[0]*(W+2) for _ in range(H+2)]
cnt = 0
for i in range(W):
    d[(0,j)] = 1
    d[(H-1,j)] = 1
for i in range(1,H-1):
    for j in range(W):
        if S[i][j] == "#":
            d[(i,j)] = 1
            cnt = 0
        else:
            d[(i,j)] = 0
            cnt += 1
            R[i][j] = cnt
            
for i in range(1,H-1):
    for j in range(W-1,-1,-1):
        if d[(i,j)] == 1:
            cnt = 0
        else:
            cnt += 1
            L[i][j] = cnt

for j in range(1,W-1):
    for i in range(H):
        if  d[(i,j)] == 1:
            cnt = 0
        else:
            cnt += 1
            D[i][j] = cnt

for j in range(1,W-1):
    for i in range(H-1,-1,-1):
        if d[(i,j)] == 1:
            cnt = 0
        else:
            cnt += 1
            U[i][j] = cnt

ans = 0
for i in range(1,H-1):
    for j in range(1,W-1):
        if ans < L[i][j] + R[i][j] + U[i][j] + D[i][j]:
            ans =  L[i][j] + R[i][j] + U[i][j] + D[i][j]
print(ans-3)


### FIXED CODE ###

H, W = map( int, input().split())
S = [["#"]*(W+2)]+[ "#"+input()+"#" for _ in range(H)]+ [["#"]*(W+2)]
H += 2
W += 2
L = [[0]*(W+2) for _ in range(H+2)]
R = [[0]*(W+2) for _ in range(H+2)]
D = [[0]*(W+2) for _ in range(H+2)]
U = [[0]*(W+2) for _ in range(H+2)]
cnt = 0

for i in range(1,H-1):
    for j in range(W):
        if S[i][j] == "#":
            cnt = 0
        else:
            cnt += 1
            R[i][j] = cnt
            
for i in range(1,H-1):
    for j in range(W-1,-1,-1):
        if S[i][j] == "#":
            cnt = 0
        else:
            cnt += 1
            L[i][j] = cnt

for j in range(1,W-1):
    for i in range(H):
        if S[i][j] == "#":
            cnt = 0
        else:
            cnt += 1
            D[i][j] = cnt

for j in range(1,W-1):
    for i in range(H-1,-1,-1):
        if S[i][j] == "#":
            cnt = 0
        else:
            cnt += 1
            U[i][j] = cnt

ans = 0
for i in range(1,H-1):
    for j in range(1,W-1):
        if ans < L[i][j] + R[i][j] + U[i][j] + D[i][j]:
            ans =  L[i][j] + R[i][j] + U[i][j] + D[i][j]
print(ans-3)
import numpy as np
from itertools import accumulate

N, M = map(int, input().split())
P = []
p = [0] * (N+1)
for i in range(M):
    p1, y1 = map(str, input().split())
    p[int(p1)] += 1
    P.append(int(p1+y1.rjust(6,"0")))
    
ans = np.argsort(P)
ans = np.argsort(ans)
p = list(accumulate(p))

for i in range(M):
    a = P[i] // 10 ** 6 
    print(int(str(a).rjust(6,"0") + str(ans[i] - p[a-1] + 1).rjust(6,"0")))

### FIXED CODE ###

import numpy as np
from itertools import accumulate

N, M = map(int, input().split())
P = []
p = [0] * (N+1)
for i in range(M):
    p1, y1 = map(str, input().split())
    p[int(p1)] += 1
    P.append(int(p1+y1.rjust(10,"0")))
    
ans = np.argsort(P)
ans = np.argsort(ans)
p = list(accumulate(p))

for i in range(M):
    a = P[i] // 10 ** 10 
    print(str(a).rjust(6,"0") + str(ans[i] - p[a-1] + 1).rjust(6,"0"))
import sys
sys.setrecursionlimit(10000000)
import numpy as np
from heapq import heapify, heappop, heappush
import copy
from math import gcd, ceil
from collections import deque
# queueとして使う場合 push, popleftを使う
from bisect import bisect_left, bisect_right
# bisect_left: 配列の順序関係が崩れない条件で挿入することができる一番左の点
# bisect_right: 配列の順序関係が崩れない条件で挿入することができる一番右の点
# A = [1, 1, 1, 2, 2, 2, 4]
# print(bisect_left(A, 2))  # 3
# print(bisect_right(A, 2))  # 6
# print(bisect_left(A, 3))  # 6
# print(bisect_right(A, 3))  # 6


def main():
    N = int(input())
    Bs = []
    As = []
    for i in range(N):
        a, b = map(int, input().split())
        As.append(a)
        Bs.append(b)
    min_med = np.median(As)
    max_med = np.median(Bs)
    if N % 2 == 0:
        print(int(2 * (max_med - min_med) + 1))
    else:
        print(int((max_med - min_med) + 1))
    


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
sys.setrecursionlimit(10000000)
import numpy as np
from heapq import heapify, heappop, heappush
import copy
from math import gcd, ceil
from collections import deque
# queueとして使う場合 push, popleftを使う
from bisect import bisect_left, bisect_right
# bisect_left: 配列の順序関係が崩れない条件で挿入することができる一番左の点
# bisect_right: 配列の順序関係が崩れない条件で挿入することができる一番右の点
# A = [1, 1, 1, 2, 2, 2, 4]
# print(bisect_left(A, 2))  # 3
# print(bisect_right(A, 2))  # 6
# print(bisect_left(A, 3))  # 6
# print(bisect_right(A, 3))  # 6


def main():
    N = int(input())
    Bs = []
    As = []
    for i in range(N):
        a, b = map(int, input().split())
        As.append(a)
        Bs.append(b)
    min_med = np.median(As)
    max_med = np.median(Bs)
    if N % 2 == 0:
        print(int(2 * (max_med - min_med) + 1))
    else:
        print(int((max_med - min_med) + 1))
    


if __name__ == '__main__':
    main()
N = 3
S = [list(map(int, input().split())) for _ in range(N)]
n_guess = int(input())
guesses = set([int(input()) for _ in range(n_guess)])
dic = {}
for r in range(N):
  for c in range(N):
    dic[S[r][c]]=(r,c)

for g in guesses:
  if g in dic:
    (r, c) = dic[g]
    S[r][c] = -1

ans = 'No'
for i in range(N):
  if sum(S[i]) == -3:
    ans='Yes'
    
for i in range(N):
  if sum([S[_][i] for _ in range(N)]) == -3:
    ans='Yes'
    
if all([S[i][N-i-1]==-1 for i in range(N)]) or \
   all([S[i][i]==-1 for i in range(N)]):
   	ans = 'Yes'
print(ans)

### FIXED CODE ###

N = int(input())
print((N+1)//2)
N, X, M = map(int, input().split())
U = [0] * (M)
U[X] = 1
S = [0] * (M)
S[1] = X
v = X
l = r = 0  # [l,r) で巡回
cycle_sum = 0
for i in range(2, M + 1):
    v = (v * v) % M
    if U[v]:
        l = U[v]
        r = i
        cycle_sum = S[i - 1] + v - S[l]
        break
    U[v] = i
    S[i] = S[i - 1] + v
if N < l:
    print(S[N])
    exit()

ans = S[l - 1]
N -= l - 1
d, m = divmod(N, r - l)
ans += d * cycle_sum
ans += S[l + m - 1] - S[l - 1]
print(ans)


### FIXED CODE ###

N, X, M = map(int, input().split())
if X == 0:
    print(0)
    exit()
U = [0] * (M + 10)
U[X] = 1
S = [0] * (M + 10)
S[1] = X
v = X
l = r = 0  # [l,r) で巡回
cycle_sum = 0
for i in range(2, M + 1):
    v = (v * v) % M
    if U[v]:
        l = U[v]
        r = i
        cycle_sum = S[i - 1] + v - S[l]
        break
    U[v] = i
    S[i] = S[i - 1] + v
if N <= l:
    print(S[N])
    exit()

ans = S[l - 1]
N -= l - 1
d, m = divmod(N, r - l)
ans += d * cycle_sum
ans += S[l + m - 1] - S[l - 1]
print(ans)
import sys
 
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()
nas = lambda: stdin.readline().split()

n, m = na()
if n <= m_:
    print("Yes")
else:
    print("No")

### FIXED CODE ###

import sys
 
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()
nas = lambda: stdin.readline().split()

n, m = na()
if n <= m:
    print("Yes")
else:
    print("No")
n = int(input())
num = list(map(int,input().split()))
numabs = [abs(i) for i in num]
cnt = 0    
negative = []        
for i in num :
    if i < 0 :
        cnt += 1
        negative.append()

if cnt % 2 == 0 :
    print(sum(numabs))

if cnt % 2 == 1 :
    print(sum(numabs)-2*max(negative))

### FIXED CODE ###

n = int(input())
num = list(map(int,input().split()))
numabs = [abs(i) for i in num]
cnt = 0           
for i in num :
    if i < 0 :
        cnt += 1

if cnt % 2 == 0 :
    print(sum(numabs))

if cnt % 2 == 1 :
    print(sum(numabs) - 2*min(numabs))
n,k = map(int, input().split())
s = list(input()) 

k_lower = s[:k-1]+s[k-1].lower()+s[k:]
print(k_lower)


### FIXED CODE ###

n,k = map(int, input().split())
s = str(input()) 

print(s[:k-1]+s[k-1].lower()+s[k:])
n,k=map(int,input().split())
s=input()
if s[k-1]=='A':
  s[k-1]='a'
elif s[k-1]=='B':
  s[k-1]='b'
else:
  s[k-1]='c':
print(s)

### FIXED CODE ###

n, k = map(int, input().split())
s = input()

print(s[:k-1]+s[k-1].lower()+s[k:])
2 3 1
1 1
1 2
2 2
1 2

### FIXED CODE ###

n, m, Q = map(int, input().split())
M = [[0]*n for _ in range(n)]
for i in range(m):
    l, r = map(int, input().split())
    M[r-1][l-1] += 1

for q in range(n):
    for p in reversed(range(1, n)):
        M[q][p-1] += M[q][p]
for p in range(n):
    for q in range(n-1):
        M[q+1][p] += M[q][p]

for i in range(Q):
    p, q = map(int, input().split())
    print(M[q-1][p-1])
import math
N, K = map(int,input().split())

N = math.gcd(N,K)

for i in range(10*6):
    N = abs(N-K)
    if N == 0:
        break

print(N)

### FIXED CODE ###

#
N, K = map(int,input().split())
N  %= K
min = N
for i in range(10**6):
    N = abs(N-K)
    if min > N:
        min = N
    if N == 0:
        min = 0
        break
print(min)
S = input()
flag = True
if S[0] != 'A':
    flag = False
elif 'C' in S[2:len(S)-1]:
    s2 = S.replace('C', '', 1)
    if 'C' in s2:
        flag = False
if flag:
    s2 = s2.replace('A', '', 1)
    if s2.islower():
        flag = True
    else:
        flag = False
if flag:
    print('AC')
else:
    print('WA')



### FIXED CODE ###

S = input()
flag = True
if S[0] != 'A':
    flag = False
elif 'C' not in S[2:len(S)-1]:
    flag = False
s2 = S.replace('C', '', 1)
if 'C' in s2:
    flag = False
    
if flag:
    s2 = s2.replace('A', '', 1)
    if s2.islower():
        flag = True
    else:
        flag = False
if flag:
    print('AC')
else:
    print('WA')
s=input()
x,y=[int(i) for i in input().split()]

if x==0:
    print("No")

### FIXED CODE ###

import math

a,b=[int(i) for i in input().split()]
print(math.ceil((a+b)/2))