Harryxun/stf_run huggingface dataset:
import sys

#readline = sys.stdin.readline
_input = open('input')
readline = _input.readline

H, W = map(int, readline().split())
S = [readline().rstrip() for i in range(H)]

vert = [[0]*W for i in range(W)]
hori = [[0]*W for i in range(W)]

for y in range(H):
    for x in range(W):

        if x > 0 and S[y][x - 1] != '#':
            hori[y][x] = hori[y][x - 1]
        elif S[y][x] == '#':
            hori[y][x] = 0
        else:
            # hori
            count = 1
            while x + count < W and S[y][x + count] == '.':
                count += 1
            hori[y][x] = count

        if y > 0 and S[y - 1][x] != '#':
            vert[y][x] = vert[y - 1][x]
        elif S[y][x] == '#':
            vert[y][x] = 0
        else:
            # hori
            count = 1
            while y + count < H and S[y + count][x] == '.':
                count += 1
            vert[y][x] = count

ans = 0
for y in range(H):
    for x in range(W):
        a = hori[y][x] + vert[y][x] - 1
        ans = max(a, ans)

print(ans)

### FIXED CODE ###

import sys

readline = sys.stdin.readline

H, W = map(int, readline().split())
S = [readline().rstrip() for i in range(H)]

vert = [[0]*W for i in range(H)]
hori = [[0]*W for i in range(H)]

for y in range(H):
    for x in range(W):

        if x > 0 and S[y][x - 1] != '#':
            hori[y][x] = hori[y][x - 1]
        elif S[y][x] == '#':
            hori[y][x] = 0
        else:
            # hori
            count = 1
            while x + count < W and S[y][x + count] == '.':
                count += 1
            hori[y][x] = count

        if y > 0 and S[y - 1][x] != '#':
            vert[y][x] = vert[y - 1][x]
        elif S[y][x] == '#':
            vert[y][x] = 0
        else:
            # hori
            count = 1
            while y + count < H and S[y + count][x] == '.':
                count += 1
            vert[y][x] = count

ans = 0
for y in range(H):
    for x in range(W):
        a = hori[y][x] + vert[y][x] - 1
        ans = max(a, ans)

print(ans)
n=int(input())
V=list(map(int,input().split()))
E=Counter(V[::2]).most_common()
O=Counter(V[1::2]).most_common()

E1=E[0][1]
O1=O[0][1]
if E[0][0]==O[0][0]:
  print(n//2)
elif E1==O1==n//2:
  print(0)
else:
  E2=E[1][1] if len(E)>1 else 0
  O2=O[1][1] if len(O)>1 else 0
  if E2!=0 and O2!=0:
    print(min(n//2-E2+n//2-O1,n//2-E1+n//2-O2))
  else:
    print(min(n//2-E2+n//2-O1,n//2-E1+n//2-O2))

### FIXED CODE ###

from collections import Counter
n=int(input())
V=list(map(int,input().split()))

E=Counter(V[::2]).most_common() + [(0, 0)]
O=Counter(V[1::2]).most_common() + [(0, 0)]
if E[0][0]!=O[0][0]:
  print(n-E[0][1]-O[0][1])
else:
  print(n-max(E[1][1]+O[0][1],E[0][1]+O[1][1]))
import bisect
 
n = int(input())
d = sorted(list(map(int,input().split())))
m = int(input())
t = list(map(int,input().split()))

flg = True
for i in range(m):
  index = bisect.bisect_left(d,t[i])
  if t[i] == d[index]:
    del d[index]
  else:
    flg = False
    break
 
print('YES' if flg else 'NO')

### FIXED CODE ###

import collections
 
n = int(input())
d = sorted(list(map(int,input().split())))
m = int(input())
t = list(map(int,input().split()))

cd = collections.Counter(d)
ct = collections.Counter(t)

flg = True
for k in ct.items():
  if k[1] > cd[k[0]]:
    flg = False

print('YES' if flg else 'NO')
import numpy as np
n = int(input())
b = list(int(x) for x in input().split())
a=np.array(b)

x = 0
for i in range(n):
    if (a[0] in np.delete(a, 0)) is True:
        print("NO")
        x = 1
        break

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


### FIXED CODE ###

s,t=input().split()
a,b=map(int,input().split())
c=input()
d=" "
if s==c:
  print("{}""{}""{}".format(a-1,d,b))
else:
  print("{}""{}""{}".format(a,d,b-1))
from collections import deque

X, Y, A, B, C = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))

p.sort(reverse = True)
q.sort(reverse = True)
r.sort(reverse = True)

r = deque(r)
deque_p = deque(p[:X])
deque_q = deque(q[:Y])
deque_r = deque([])

while min(deque_p[-1], deque_q[-1]) < r[0]:
    if deque_p[-1] <= deque_q[-1]:
        deque_p.pop()
        deque_r.append(r.popleft())
    else:
        deque_q.pop()
        deque_r.append(r.popleft())

    if not r:
        break

ans = sum(deque_p) + sum(deque_q) + sum(deque_r)

print(ans)


### FIXED CODE ###

from collections import deque

X, Y, A, B, C = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))

p.sort(reverse = True)
q.sort(reverse = True)
r.sort(reverse = True)

r = deque(r)
deque_p = deque(p[:X])
deque_q = deque(q[:Y])
deque_r = deque([])

if r:
    while min(deque_p[-1], deque_q[-1]) < r[0]:
        if deque_p[-1] <= deque_q[-1]:
            deque_p.pop()
            deque_r.append(r.popleft())
        else:
            deque_q.pop()
            deque_r.append(r.popleft())

        if (not r) or (not deque_p) or (not deque_q):
            break

if r and deque_p:
    while deque_p[-1] < r[0]:
        deque_p.pop()
        deque_r.append(r.popleft())

        if (not r) or (not deque_p):
            break

if r and deque_q:
    while deque_q[-1] < r[0]:
        deque_q.pop()
        deque_r.append(r.popleft())

        if (not r) or (not deque_q):
            break
    

ans = sum(deque_p) + sum(deque_q) + sum(deque_r)

print(ans)
hoge = [int(x) for x in input().split()]
moji = ''
char = {}
for i in range(hoge[0]):
    moji += input()
for m in moji:
    if m in char:
        char[m] += 1
    else:
        char.update({m:1})

four = 0
two = 0
one = 0
for c in char:
    while(char[c] > 0):
        four += char[c] / 4:
        two += char[c]%4/2:
        one += char[c]%2:

if four*4+two*2+one == hoge[0]*hoge[1]:
    print("Yes")
else:
    print("No")

### FIXED CODE ###

present = input()
if(present[:4] == 'YAKI'):
    print("Yes")
else:
    print("No")
string=list(input())
c=0
a=0
for s in string:
  if int(s)==0 and c=1:
    c-=1
  elif int(s)==0 and c=0:
    c+=1
    a+=1
  elif int(s)==1 and c=0:
	c+=1
  elif int(s)==1 and c=1:
	c-=1
    a+=1
print(a)

### FIXED CODE ###

string=list(input())
if int(string[0])==1:
    c=0
else:
    c=1
a=0
for s in string:
    if int(s)==0 and c==1:
        c-=1
    elif int(s)==0 and c==0:
        c+=1
        a+=1
    elif int(s)==1 and c==0:
        c+=1
    elif int(s)==1 and c==1:
        c-=1
        a+=1
print(a)
import sys
def input():
    return sys.stdin.readline()[:-1]
INF = float("inf")

# Binary Indexed Tree
class BIT:
    def __init__(self, N):
        self.N = N
        self.bit = [0] * (N+1)
    def add(self, x, a):
        while x <= self.N:
            self.bit[x] += a
            x += x & -x
    def sum(self, x):
        ret = 0
        while x != 0:
            ret += self.bit[x]
            x -= x & -x
        return ret

N = int(input())
S = list(input())
Q = int(input())
bit = [BIT(N) for i in range(26)]
for i in range(N):s
    bit[ord(S[i])-97].add(i+1, 1)
for i in range(Q):
    q, x, y = input().split()
    q = int(q)
    if q == 1:
        i = int(x)-1
        bit[ord(S[i])-97].add(i+1, -1)
        S[i] = y
        bit[ord(y)-97].add(i+1, 1)
    else:
        l, r, ans = int(x), int(y), 0
        for i in range(26):
            ans += (bit[i].sum(r)-bit[i].sum(l-1))> 0
        print(ans)


### FIXED CODE ###

import sys
def input():
    return sys.stdin.readline()[:-1]
INF = float("inf")

# Binary Indexed Tree
class BIT:
    def __init__(self, N):
        self.N = N
        self.bit = [0] * (N+1)
    def add(self, x, a):
        while x <= self.N:
            self.bit[x] += a
            x += x & -x
    def sum(self, x):
        ret = 0
        while x != 0:
            ret += self.bit[x]
            x -= x & -x
        return ret

N = int(input())
S = list(input())
Q = int(input())
bit = [BIT(N+1) for i in range(26)]
for i in range(N):
    bit[ord(S[i])-97].add(i+1, 1)
for i in range(Q):
    q, x, y = input().split()
    q = int(q)
    if q == 1:
        i = int(x)-1
        bit[ord(S[i])-97].add(i+1, -1)
        S[i] = y
        bit[ord(y)-97].add(i+1, 1)
    else:
        l, r, ans = int(x), int(y), 0
        for i in range(26):
            ans += (bit[i].sum(r)-bit[i].sum(l-1))> 0
        print(ans)
a,b,k = map(int,input().split())
ans = [1]
for i in range(2,max(a,b)):
    if a%i==0 and b%i==0:
        ans.append(i)
ans.sort(reverse = True)
print(ans[k-1])

### FIXED CODE ###

a,b,k = map(int,input().split())
ans = [1]
for i in range(2,min(a,b)+1):
    if a%i==0 and b%i==0:
        ans.append(i)
ans.sort(reverse = True)
print(ans[k-1])
for _ in[0]*int(input()):
 M=''.join(sorted(input()))
 print(int(M[::-1])-int(M.replace('0','')))


### FIXED CODE ###

for _ in[0]*int(input()):
 M=''.join(sorted(input()))
 print(int(M[::-1])-int(M))
def fill0(s,x,y):
	try: A[y][x] = 0
	except: pass
	if x < W-1 and A[y][x+1] == s: fill0(s,x+1,y)
	if y < H-1 and A[y+1][x] == s: fill0(s,x,y+1)
	if x > 0   and A[y][x-1] == s: fill0(s,x-1,y)
	if y > 0   and A[y-1][x] == s: fill0(s,x,y-1)

while 1:
	H,W = map(int,raw_input().split())
	if H == 0: break
	A = [list(raw_input()) for i in range(H)]
	ans = 0
	for y in range(H):
		for x in range(W):
			if A[y][x] in ["#","@","*"]:
				fill0(A[y][x],x,y)
				ans += 1
	print ans

### FIXED CODE ###

import sys
sys.setrecursionlimit(20000)
def solve(h,w,count,c):
    if 0 <= h < H and 0 <= w < W and A[h][w] == c:
        A[h][w] = count
        for dh,dw in zip([1,0,-1,0],[0,1,0,-1]):
            solve(h+dh,w+dw,count,c)
while 1:
    H,W = map(int,raw_input().split())
    if H == W == 0: break
    A = [list(raw_input()) for _ in range(H)]
    count = 0
    for h in range(H):
        for w in range(W):
            if type(A[h][w]) is not int:
                solve(h,w,count,A[h][w])
                count += 1
    print count
import numpy
n,k = map(int,input().split())
s = input()
tmp = s[0]
cnt = 1
ans = 0
ss = []
res = [0]
sum = 0
if s[0] == '0':
    ss.append(0)

for i in range(1,n):
    if s[i] == tmp:
        cnt += 1
    else:
        tmp = s[i]
        ss.append(cnt)
        cnt = 1
ss.append(cnt)
if s[-1] == '0':
    ss.append(0)
ll = len(ss)
for i in range(ll):
    sum += ss[i]
    res.append(sum)

if ll//2 <= k:
    print(n)
else:
    for i in range(ll-k*2)[::2]:
        ans = max(ans, res[i+k*2+1]-res[i])
    print(ans)


### FIXED CODE ###

n,k = map(int,input().split())
s = input()
tmp = s[0]
cnt = 1
ans = 0
ss = []
res = [0]
sum = 0
if s[0] == '0':
    ss.append(0)

for i in range(1,n):
    if s[i] == tmp:
        cnt += 1
    else:
        tmp = s[i]
        ss.append(cnt)
        cnt = 1
ss.append(cnt)
if s[-1] == '0':
    ss.append(0)
ll = len(ss)
for i in range(ll):
    sum += ss[i]
    res.append(sum)

if ll//2 <= k:
    print(n)
else:
    for i in range(ll-k*2)[::2]:
        ans = max(ans, res[i+k*2+1]-res[i])
    print(ans)
N = int(input())
a = 26
counter = 1
while a<N:
  N -= a
  counter += 1
  a *= 26
ans = []
N -= 1
while N>26:
  ans.append(N%26)
  N = N //26
ans.append(N)
#print(ans)
dic = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
ans = [dic[i] for i in ans[::-1]]
n = len(ans)
print("a"*(counter-n) + "".join(ans))

### FIXED CODE ###

N = int(input())
a = 26
counter = 1
while a<N:
  N -= a
  counter += 1
  a *= 26
ans = []
N -= 1
while N>=26:
  ans.append(N%26)
  N = N //26
ans.append(N)
dic = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
ans = [dic[i] for i in ans[::-1]]
n = len(ans)
print("a"*(counter-n) + "".join(ans))
s = int(input())

h = int((s - s % 3600 ) / 3600 )
m = int((s% 3600 - (s % 3600)  % 60) /60 )
s = int((s % 3600)  % 60 )/ 60) 
print ( "h:m:s")

### FIXED CODE ###

s = int(input())

h = str(int((s - s % 3600 ) / 3600 ))
m = str(int((s% 3600 - (s % 3600)  % 60) /60 ))
s = str(int((s % 3600)  % 60 ))
print ( h + ":" + m + ":" + s)
N, C = map(int, input().split())
timeTable = [tuple(map(int, input().split())) for i in range(N)]
sutyoku =  [set() for i in range(20003)]

for s,t,c in timeTable:
    if s == 0:
        for i in range(2*t):
            sutyoku[i].add(c)
    else:
        for i in range(2*s-1,2*t):
            sutyoku[i].add(c)

ans = 0
for s in sutyoku:
    ans = max(ans, len(s))

print(ans)



### FIXED CODE ###

import numpy

N, C = map(int, input().split())
STC = [list(map(int, input().split())) for i in range(N)]
Channel = numpy.array([[0]*(2*10**5+1) for i in range(C+1)])

for s, t, c in STC:
    Channel[c][2*s-1] += 1
    Channel[c][2*t] -= 1
    
for i in range(len(Channel)):
    Channel[i] = numpy.cumsum(Channel[i])
bool_Channel = Channel.astype(numpy.bool_)

needs = numpy.sum(bool_Channel, axis=0)
print(needs.max())
a,b,c = map(int,input().split)
print(min(b//a,c))


### FIXED CODE ###

a,b,c = map(int,input().split())
if b//a>=c: print(c)
else: print(b//a)
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

#素因数分解
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

s = inp()
fa = prime_factorize(s)
h = 1
# print(fa)
n = len(fa)
if n >= 4:
    for i in range(n//4):
        # print(i,fa[i])
        h *= fa[i]
    for i in range(n-1,n-n//4-1,-1):
        # print(i,fa[i])
        h *= fa[i]
else:
    h = fa[-1] 
w = s//h

print(0,0,w,0,0,h)






### FIXED CODE ###

from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

s = inp()
y3 = math.ceil(s/(10**9))
x3 = 10**9*y3 - s
print(0,0,10**9,1,x3,y3)
# C
x, n = map(int, input().split())
p = list(map(int, input().strip().split()))
print(p)
ans = 0
if n == 0:
    print(x)
elif x not in p:
    print(x)
else:
    for i in range(102):
        if i not in p:
        if abs(x - i) < abs(x - ans):
                ans = i
    print(ans)


### FIXED CODE ###

# C
x, n = map(int, input().split())
p = list(map(int, input().split()))
# print(p)
ans = 0
if n == 0:
    print(x)
elif x not in p:
    print(x)
else:
    for i in range(102):
        if i not in p:
            if abs(x - i) < abs(x - ans):
                ans = i
    print(ans)
a, b = map(int, input().split(' '))
sum = 0
for i in range(1,2):
	if a < b:
      sum += b
      b -= 1
    else:
      sum += a
      a -= 1
print('sum')

### FIXED CODE ###

A,B = map(int, input().split())
sum = 0
for i in range(2):
  if A < B:
    sum += B
    B -= 1
  else:
    sum += A
    A -= 1
print(sum)
# -*- coding: utf-8 -*-
l = int(input())
if l <=9:
  print("{}".format(a));
  return
if l <=99:
  print('9')
  return;
if l < 999:
  print("{}".format(9+a-99))
  return
if l <= 9999:
  print('909')
  return
print("{}".format(909+a-9999))

### FIXED CODE ###

a = int(input())
if a <10:
  print("{}".format(a))
elif a <100:
  print("9")
elif a < 1000:
  print("{}".format(9+a-99))
elif a < 10000:
  print('909')
elif a < 100000:
  print("{}".format(909+a-9999))
else:
  print('90909')
N, L = map(int, input().split())
s = []
for i in N:
  s.append(i+L)
t = abs(s)
del s[t.index(min(s))]
print(sum(s))

### FIXED CODE ###

N, L = map(int, input().split())
s = []
for i in range(N):
  s.append(i+L)
t = list(map(abs, s))
del s[t.index(min(t))]
print(sum(s))
n,a,b=map(int,input(),split())
print(min(n*a,b))


### FIXED CODE ###

n,a,b=map(int,input().split())
print(min(n*a,b))
7
6 7 8
8 8 3
2 5 2
7 8 6
4 6 8
2 3 4
7 5 1

### FIXED CODE ###

import numpy as np
#N,K = map(int,input().split())
N = int(input())
A,B,C = 0,0,0
for i in range(N):
  a,b,c = map(int,input().split())
  A,B,C = a+max(B,C),b+max(A,C),c+max(A,B)
print(max(A,B,C))
print(input().count(2))

### FIXED CODE ###

n = input()
n = list(n)
print(n.count('2'))
import sys
for line in sys.stdin:
    N = int(raw_input())
    ans = 0
    for a in xrange(10):
        for b in xrange(10):
            for c in xrange(10):
                for d in xrange(10):
                    if a+b+c+d == D:
                        ans += 1
    print ans

### FIXED CODE ###

import sys
for line in sys.stdin:
    N = int(line)
    ans = 0
    for a in xrange(10):
        for b in xrange(10):
            for c in xrange(10):
                for d in xrange(10):
                    if a+b+c+d == N:
                        ans += 1
    print ans
s1, s2, s3 =　map( str, input().split() )
print( "A" + list(s2)[0] + "C")

### FIXED CODE ###

s1, s2, s3 = map( str, input().split() )
print( "A" + s2[0] + "C" )
def func(n,k):
    SUM = 0
    A = [int(input()) for _ in range(n)]
    for i in range(k-1):
        SUM += A[i]
    ans = SUM
    for i in range(k,len(A)):
        ans = max(ans,SUM+A[i]-A[i-k])
    print(ans)

for _ in range(10)
    n,k = [int(s) for s in input().split()]
    if n == 0 and k ==0:
        break
    else:
        func(n,k)


### FIXED CODE ###

def func(n,k):
    SUM = 0
    ans = 0
    A = [int(input()) for _ in range(n)]
    for i in range(k):
        SUM += A[i]
    for i in range(k,len(A)):
        ans = max(ans,SUM+A[i]-A[i-k])
        SUM = SUM+A[i]-A[i-k]
    print(ans)

for _ in range(10):
    n,k = [int(s) for s in input().split()]
    if n == 0 and k ==0:
        break
    else:
        func(n,k)
a,b,c=map(int,input().split())
if a[-1] == b[0] and b[-1] == c[0]:
  print("YES")
else:
  print("NO")

### FIXED CODE ###

a,b,c=map(str,input().split())
if a[-1] == b[0] and b[-1] == c[0]:
  print("YES")
else:
  print("NO")
A,B,C,D = map(int,input().split())
if (A+B) = (C+D):
  print("Balanced")
elif (A+B) < (C+D):
  print("Right")
else:
  print("Left")


### FIXED CODE ###

A,B,C,D = map(int,input().split())
if (A+B) == (C+D):
  print("Balanced")
elif (A+B) < (C+D):
  print("Right")
else:
  print("Left")
a, b = (int(_) for _ in input().split())
total = 1
ans = 0
if b == 1:
    print(1)
else:
    while total < b:
      	total -= 1
        total += a
        ans += 1
        
print(ans)

### FIXED CODE ###

a, b = (int(_) for _ in input().split())
total = 1
ans = 0
while total < b:
  total -= 1
  total += a
  ans += 1    
print(ans)
n = int(input())
test = []
for i in range(n):
    test.append(input())
print('AC x ' + str(test.count('AC')))
print('NA x ' + test.count('NA'))
print('TLE x ' + test.count('TLE'))
print('RE x ' + test.count('RE'))

### FIXED CODE ###

n = int(input())
if n%1000 == 0:
    print(0)
else:
    print(1000 - n % 1000)
def swap(a, b):
    tmp = a
    a = b
    b = tmp
    return a, b


def solve(): # 072D- Derangement
    N = int(input())
    p = list(map(int, input().split(" ")))
    p.insert(0, 1)
    cnt = 0

    for i in range(1, N+1):
        if i == p[i]:
            p[i], p[i+1] = swap(p[i], p[i+1])
            cnt += 1
        elif i == N:
            p[i-1], p[i] = swap(p[i-1], p[i])
            cnt += 1
        else:
            pass

    print(cnt)
    
    
solve()

### FIXED CODE ###

def swap(a, b):
    tmp = a
    a = b
    b = tmp
    return a, b


def solve(): # 072D- Derangement
    N = int(input())
    p = list(map(int, input().split(" ")))
    p.insert(0, 0)
    cnt = 0

    for i in range(1, N+1):
        if i == p[i] and i != N:
            p[i], p[i+1] = swap(p[i], p[i+1])
            cnt += 1
        elif i == N and i == p[i]:
            p[i-1], p[i] = swap(p[i-1], p[i])
            cnt += 1
        else:
            pass

    print(cnt)
    
    
solve()
n,m =map(int, input().split())  #複数数値入力
x=list(map(int, input().split()))  #リスト入力

x.sort()
x_d = list()

if m ==1:
    print(0)
else:
    for i in range(m-1):
        x_d.append(x[i+1] - x[i])

    print(x,x_d)

    for i in range(n-1):
        x_d.remove(max(x_d))
    sumd = sum(x_d)  
    print(sumd)

### FIXED CODE ###

import numpy as np
n,m =map(int, input().split())  #複数数値入力
x=list(map(int, input().split()))  #リスト入力

x.sort()
x_d = list()

if n > m:
    print(0)
else:
    if m == 1:
        print(0)
    else:
#         for i in range(m-1):
#             x_d.append(x[i+1] - x[i])   
        x_d = list(np.diff(x))

#         for i in range(n-1):
            
#             x_d.remove(max(x_d))
            
        x_d.sort(reverse=True)
#         sumd = sum(x_d)  
        sumd = sum(x_d[n-1:])
        print(sumd)
N = int(input())
v = list(map(int,input().split()))
v_even = [0 for i in range(max(v)+1)]
v_odd = [0 for i in range(max(v))+1]
sum_even = 0
sum_odd = 0
for i in range(N):
    if i%2 :
        v_odd[v[i]] += 1
        sum_odd += 1
    else:
        v_even[v[i]] += 1
        sum_even += 1
max_odd = 0
odd = 0
max_even = 0
even = 0
for i in range(len(v_odd)):
    if v_odd[i] > max_odd:
        max_odd = v_odd[i]
        odd = i

for i in range(len(v_even)):
    if v_even[i] > max_even:
        max_even = v_even[i]
        even = i

if odd != even:
    ans = sum_even - max_even + sum_odd - max_odd
else:
    v_odd[odd] = 0
    v_even[even] = 0
    ans = min((sum_even - max_even + sum_odd - max(v_odd)), sum_even - max(v_even) + sum_odd - max_odd)

print(ans)

### FIXED CODE ###

N = int(input())
v = list(map(int,input().split()))
v_even = [0 for i in range(max(v)+1)]
v_odd = [0 for i in range(max(v)+1)]
sum_even = 0
sum_odd = 0
for i in range(N):
    if i%2 :
        v_odd[v[i]] += 1
        sum_odd += 1
    else:
        v_even[v[i]] += 1
        sum_even += 1
max_odd = 0
odd = 0
max_even = 0
even = 0
for i in range(len(v_odd)):
    if v_odd[i] > max_odd:
        max_odd = v_odd[i]
        odd = i

for i in range(len(v_even)):
    if v_even[i] > max_even:
        max_even = v_even[i]
        even = i

if odd != even:
    ans = sum_even - max_even + sum_odd - max_odd
else:
    v_odd[odd] = 0
    v_even[even] = 0
    ans = min((sum_even - max_even + sum_odd - max(v_odd)), sum_even - max(v_even) + sum_odd - max_odd)

print(ans)
import numpy as np

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

lc = mp.lcm.reduce(a)

res = 0
for i in range(n):
    res = res + lc / a[i]
    res = res % 1000000007

print(res)



### FIXED CODE ###

import fractions
def LCM(a, b):
    return(a // fractions.gcd(a, b) * b)

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

for i in range(n):
    lc = LCM(lc, a[i])

res = 0;
for i in range(n):
    res = res + (lc // a[i])

print(res % 1000000007)
H, W, D = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
Q = int(input())
pos = [None]*(H*W+1)
for i, elem in enumerate(A):
    for j, a in enumerate(elem):
        pos[a] = (i, j)
acc = [[] for _ in range(D+1)]
for i in range(1, D+1):
    tmp = i
    s = 0
    acc[i].append(s)
    while tmp <= H*W-D:
        s += abs(pos[tmp][0]-pos[tmp+D][0])+abs(pos[tmp][1]-pos[tmp+D][1])
        acc[i].append(s)
        tmp += D

for _ in range(Q):
    L, R = map(int, input().split())
    print(acc[(R-1) % D+1][(R-R % D-1)//D+1] -
          acc[(R-1) % D+1][(L-L % D-1)//D+1])


### FIXED CODE ###

H, W, D = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
Q = int(input())
pos = [None]*(H*W+1)
for i, elem in enumerate(A):
    for j, a in enumerate(elem):
        pos[a] = (i, j)
acc = [[] for _ in range(D+1)]
for i in range(1, D+1):
    tmp = i
    s = 0
    acc[i].append(s)
    while tmp <= H*W-D:
        s += abs(pos[tmp][0]-pos[tmp+D][0])+abs(pos[tmp][1]-pos[tmp+D][1])
        acc[i].append(s)
        tmp += D

for _ in range(Q):
    L, R = map(int, input().split())
    print(acc[(R-1) % D+1][(R-R % D)//D-(R % D == 0)] -
          acc[(R-1) % D+1][(L-L % D)//D-(L % D == 0)])