Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

# 5
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


n = I()
s = S()
ciphers = [f'{i:03}' for i in range(1000)]
ans = 0
for v in ciphers:
    j = 0
    for i in range(n):
        if s[i] == v[j]:
            j += 1
        if j == 3:
            ans += 1
            break
print(ans)


### FIXED CODE ###

# 5
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


n = I()
s = S()
ciphers = [f'{i:03}' for i in range(1000)]
ans = 0
for v in ciphers:
    j = 0
    for i in range(n):
        if s[i] == v[j]:
            j += 1
        if j == 3:
            ans += 1
            break
print(ans)
### BUGGY CODE ###

##N T
#t1 t2 ... tN
N, T = map(int,input().split())
a = list(map(int, input().split()))


b = []

for i in range(N):
    set1 = set()
    for j in range(T):
        set1.add(a[i] + j)
    b.append(set1)
d = set()
for n in range(len(b)):
    d = d | b[n]

print(len(d))

### FIXED CODE ###

##N T
#t1 t2 ... tN
N, T = map(int,input().split())
a = list(map(int, input().split()))
b = []
zo  = [0, T]
leng = T
"""
d = set()
for i in range(N):
    d = d | set(range(a[i], a[i] + T))
"""
for i in range(N):
    if  a[i] + T <= zo[1]:
        pass
    elif a[i] <= zo[1] and zo[1] <= a[i] + T:
        leng += a[i] + T - zo[1]
        zo[1] = a[i] + T
    elif zo[1] <= a[i]:
        zo[0] = a[i]
        zo[1] = a[i] + T
        leng += T






print(leng)
### BUGGY CODE ###

S = input().split(" ")
n = int(S[0])
k = int(S[1])
arr = []
for i in range(n):
    arr.append(int(input()))

def calculate(n,k,arr):
    arr.sort()
    drr = []
    for i in range(1,n):
        drr.append(arr[i] - arr[i-1])

    i = 0
    sums = []
    while (i + k-1) <= len(drr):
        tmp = sum(drr[i:i + k-1])
        sums.append(tmp)
        i = i + 1
    print(min(sums))

calculate(n,k,arr)

### FIXED CODE ###

S = input().split(" ")
n = int(S[0])
k = int(S[1])
arr = []
for i in range(n):
    arr.append(int(input()))

def calculate(n, k, arr):
    arr.sort()
    drr = []
    for i in range(1, n):
        drr.append(arr[i] - arr[i - 1])

    j = 0
    s = 0
    sums = []
    for i in range(len(drr) - (k - 1) + 1):
        while j < i + (k-1):
            s = s + drr[j]
            j = j + 1
        sums.append(s)
        s = s - drr[i]
    print(min(sums))



calculate(n, k, arr)
### BUGGY CODE ###

N = int(input())
a_list = list(map(int, input().split()))
check = 0
for i in range(len(a_list)):
    val = a_list[i]
    if a_list.count(val) > 1:
        check = 1
        
if check == 0:
    print("YES")
else:
    print("NO")

### FIXED CODE ###

N = int(input())
a_list = list(map(int, input().split()))
a_list.sort()
check = 0
for i in range(N-1):
    if a_list[i] == a_list[i+1]:
        check = 1
        break
        
if check == 0:
    print("YES")
else:
    print("NO")
### BUGGY CODE ###

import sys
sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7


def f(index, l):
    if index == len(l):
        return 1
    if l[index] == '1':
        return ( 3 ** (len(l)-1-index) + 2 * f(index+1, l) ) % MOD
    else:
        return f(index+1, l) % MOD


def main():
    l = input()
    print(f(0, l))

main()

### FIXED CODE ###

import sys
sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7


def f(index, l):
    if index == len(l):
        return 1
    if l[index] == '1':
        return ( 3 ** (len(l)-1-index) + 2 * f(index+1, l) ) % MOD
    else:
        return f(index+1, l) % MOD


def main():
    l = input()
    n = len(l)
    one = 0
    ans = 0
    for i in range(n):
        if l[i] == '1':
            ans += pow(2, one, MOD) * pow(3, n-1-i, MOD)
            ans %= MOD
            one += 1
    ans += pow(2, one, MOD)
    ans %= MOD
    print(ans)

main()
### BUGGY CODE ###


from collections import deque
N=int(input())
T=[[] for _ in range(N)]
for i in range(N-1):
    a,b,c=map(int, input().split())
    T[a-1].append((b-1,c))
    T[b-1].append((a-1,c))

Q,K=map(int, input().split())
que=[(K-1,0)]
done=[-1]*N
while que:
    t=que.pop(0)
    now,nowd=t
    done[now]=nowd
    for nex in T[now]:
        nexnode,nexd=nex
        if done[nexnode]!=-1:
            continue
        else:
            que.append((nexnode, nowd+nexd))

#print(done)
for i in range(Q):
    x,y=map(int, input().split())
    print(done[x-1]+done[y-1])

### FIXED CODE ###

N=int(input())
import sys
sys.setrecursionlimit(10**6)
T=[[] for _ in range(N)]
for i in range(N-1):
    a,b,c=map(int, input().split())
    T[a-1].append((b-1,c))
    T[b-1].append((a-1,c))

Q,K=map(int, input().split())
D=[-1]*N
D[K-1]=0
def dfs(start, nowd):
    to=T[start]
    for t in to:
        next_node,c=t
        if D[next_node]!=-1:
            continue
        nextd=nowd+c
        D[next_node]=nextd
        dfs(next_node, nextd)
dfs(K-1,0)
for i in range(Q):
    x,y=map(int, input().split())
    print(D[x-1]+D[y-1])
### BUGGY CODE ###

H, W, M = map(int, input().split())
A = [0]*H
B = [0]*W
D = set()
for _ in range(M):
    h,w = map(int, input().split())
    A[h-1] += 1
    B[w-1] += 1
    D.add((h-1,w-1))
a, b = max(A), max(B)
res = a+b-1
for i in range(H):
    for j in range(W):
        if (A[i],B[j]) == (a,b):
            if (i,j) not in D:
                print(res+1)
                exit()
print(res)

### FIXED CODE ###

H, W, M = map(int, input().split())
A = [0]*H
B = [0]*W
D = set()
for _ in range(M):
    h,w = map(int, input().split())
    A[h-1] += 1
    B[w-1] += 1
    D.add((h-1,w-1))
a, b = max(A), max(B)
res = a+b-1
E, F = [], []
for i in range(H):
    if A[i] == a:
        E.append(i)
for j in range(W):
    if B[j] == b:
        F.append(j)
for i in E:
    for j in F:
        if (i,j) not in D:
            print(res+1)
            exit()
print(res)
### BUGGY CODE ###

from bisect import bisect_left
N = int(input())
L = list(map(int, input().split()))
L.sort()
cnt = 0
for b in range(N):
    for a in range(b):
        print()
        ab = L[a] + L[b]
        r = bisect_left(L, ab)
        cnt += r - (b + 1) 
print(cnt)

### FIXED CODE ###

from bisect import bisect_left
 
N = int(input())
L = list(map(int, input().split()))
 
L.sort()
 
cnt = 0
 
# 2番目に長い棒bを固定
for b in range(N):
    # bより短い棒aを一つずつ探索
    for a in range(b):
        ab = L[a] + L[b]
        # 二分探索でa+bより短い棒（c<a+bが成り立つc）の範囲を求める
        r = bisect_left(L, ab)
        # cの範囲のうちbの範囲を除外して、cの本数を求める
        cnt += r - (b + 1)
 
print(cnt)
### BUGGY CODE ###

#!/usr/bin/env python3
n, p = map(int, input().split())
k = c = f = 1
while f <= p:
    if p % f == 0:
        k = c
    c += 1
    f = c**n
print(k)

### FIXED CODE ###

n,p=map(int,input().split());a=1
print(n<2and p or exec("for i in range(2,int(p**(1./n))+1):\n while p%(i**n)<1:a*=i;p//=i**n")or a)
### BUGGY CODE ###

N=int(input())
A=list(input())

for i in range(1,N+1):
	if str(i) in A:
		print(A.count(str(i)))
	else:
		print("0")


### FIXED CODE ###

N = int(input())
K = [0]*N
A = [int(i) for i in input().split()]

for i in range(N-1):
    K[A[i]-1] += 1
for i in K:
    print(i)
### BUGGY CODE ###

N = int(input())
string = []
count = 0
s = []
for i in range(0, N):
    s = input()
    if not s in string:
        string.append(s)
    
print(len(string))

### FIXED CODE ###

N = int(input())
dic = {}
s = []
for i in range(0,N):
    s = input()
    if not s in dic:
        dic[s] = 1

print(len(dic))
### BUGGY CODE ###

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

#和がx以上になる組み合わせの総数
def pattern_num_ge_x(x):
    p=0
    for a in A:
        ng=-1
        ok=N
        while (ok-ng)>1:
            m=(ok+ng)//2
            if a+A[m]>=x:
                ok=m
            else:
                ng=m
        p+=N-ok
    return(p)

#pattern_num_ge_x(x)がM未満になる最小のxを探す(=r)
#lはM以上になる最大のx
l=-1
r=10**10+1
while r-l>1:
    m=(r+l)//2
    if pattern_num_ge_x(m)<M:
        r=m
    else:
        l=m
#print(l,r)
#for i in range(300):
#    print(i,pattern_num_ge_x(i),sep=',')

#和がr以上になる組み合わせの総和を求める(累積和を使用)
S=list(reversed(A))
for i in range(1,N):
    S[i]+=S[i-1]
#print(S)
ans=0
for a in A:
    ng=-1
    ok=N
    while (ok-ng)>1:
        m=(ok+ng)//2
        if a+A[m]>=r:
            ok=m
        else:
            ng=m
    ans+=a*(N-ok) + (S[N-ok-1] if N-ok-1>=0 else 0)
    M-=(N-ok)
    #print(a,ok,a*(N-ok),ans)

#回数のあまり分lを足す
ans+=l*M

print(ans)

### FIXED CODE ###

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

#和がx以上になる組み合わせの総数
def pattern_num_ge_x(x):
    p=0
    for a in A:
        ng=-1
        ok=N
        while (ok-ng)>1:
            m=(ok+ng)//2
            if a+A[m]>=x:
                ok=m
            else:
                ng=m
        p+=N-ok
    return(p)

#pattern_num_ge_x(x)がM未満になる最小のxを探す(=r)
#lはM以上になる最大のx
l=-1
r=10**10+1
while r-l>1:
    m=(r+l)//2
    if pattern_num_ge_x(m)<M:
        r=m
    else:
        l=m
#print(l,r)
#for i in range(300):
#    print(i,pattern_num_ge_x(i),sep=',')

#和がr以上になる組み合わせの総和を求める(累積和を使用)
S=list(reversed(A))
for i in range(1,N):
    S[i]+=S[i-1]
#print(S)
ans=0
for a in A:
    ng=-1
    ok=N
    while (ok-ng)>1:
        m=(ok+ng)//2
        if a+A[m]>=r:
            ok=m
        else:
            ng=m
    ans+=a*(N-ok) + (S[N-ok-1] if N-ok-1>=0 else 0)
    M-=(N-ok)
    #print(a,ok,a*(N-ok),ans)

#回数のあまり分lを足す
ans+=l*M

print(ans)
### BUGGY CODE ###

n = int(input())
s = [0 for i in range(n)]
dics = {}
for i in range(n):
    try:
        s = input()
        a = dics[s]
        dics[s] += 1
    except KeyError:
        dics[s] = 1
max_v = max(dics.values())
max_k_list = [kv[0] for kv in dics.items() if kv[1] == max(dics.values())]
max_k_list.sort()
for i in max_k_list:
    print(i)

### FIXED CODE ###

n = int(input())
dics = {}
for i in range(n):
    try:
        s = input()
        dics[s] += 1
    except KeyError:
        dics[s] = 1
max_v = max(dics.values())

max_k_list = [kv[0] for kv in dics.items() if kv[1] == max_v]
max_k_list.sort()
for i in max_k_list:
    print(i)
### BUGGY CODE ###

N = int(input())
P = [int(x) for x in input().split()]

ans = 1
for i in range(1, N):
    tmp = 1
    j = 0
    while j < i:
        if P[i] > P[j]:
            tmp = 0
            break
        j += 1
    ans += tmp
print(ans)

### FIXED CODE ###

N = int(input())
P = [int(x) for x in input().split()]

ans = 1
P_min = P[0]
for i in range(1, N):
    if P[i] < P_min:
        ans += 1
    P_min = min(P_min, P[i])
print(ans)
### BUGGY CODE ###

txt = input()
n = (int)(input())
count = 0
sentou = ""
saigo = ""
for i in range(n):
  temp = input()
  if temp[0] == '1':
    count += 1
  else:
    sec, moji = (int)(temp.split(" ")[1]), temp.split(" ")[2]
    if (sec + count) % 2 == 1:
      sentou = moji + sentou
    else:
      saigo += moji
if count % 2 == 1:
  txt = saigo[::-1] + txt[::-1] + sentou[::-1]
else:
  txt = sentou + txt + saigo
print(txt) 

### FIXED CODE ###

txt = input()
n = (int)(input())
count = 0
sentou = ""
saigo = ""
for i in range(n):
  temp = input()
  if temp[0] == '1':
    count += 1
  else:
    sec, moji = (int)(temp.split(" ")[1]), temp.split(" ")[2]
    if (sec + count) % 2 == 1:
      sentou = moji + sentou
    else:
      saigo += moji
if count % 2 == 1:
  txt = saigo[::-1] + txt[::-1] + sentou[::-1]
else:
  txt = sentou + txt + saigo
print(txt)
### BUGGY CODE ###

# ABC 065: B – Trained?
n = int(input())
a = [int(input()) - 1 for _ in range(n)]

lightned = []
lightning = 0

while a[lightning] not in lightned:
    if a[lightning] == 1:
        break

    lightned.append(a[lightning])
    lightning = a[lightning]

print(len(lightned) + 1 if a[lightning] == 1 else -1)

### FIXED CODE ###

# ABC 065: B – Trained?
n = int(input())
a = [int(input()) for _ in range(n)]
lighting = 1
number = 0

for i in range(n):
    if lighting == 2:
        print(number)
        break
    else:
        number += 1
        lighting = a[lighting - 1]
else:
    print(-1)
### BUGGY CODE ###

H, W = map(int,input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())
c = [["#"]*(W+4) for _ in range(H+4)]
for y in range(H):
    c[y+2] = *list("##"+input()+"##"), 
inf = H*W
m = [[inf]*W for _ in range(H)]
m[ch-1][cw-1] = 0
drs = [[-1,0],[1,0],[0,0],[0,-1],[0,1]]
warps = [[-2,-2],[-2,-1],[-2,0],[-2,1],[-2,2],
        [2,-2],[2,-1],[2,0],[2,1],[2,2],
        [-1,-2],[0,-2],[1,-2],
        [-1,2],[0,2],[1,2],
        [-1,-1],[-1,1],[1,-1],[1,1]
       ]

s = 0
yx = [[ch+1, cw+1]]
notgoal = True
yxwarp = True
while notgoal and yxwarp:
    yxwarp = []
    while yx:
        yx2 = []
        for y,x in yx:
            for dy, dx in drs:
                x2 = x+dx
                y2 = y+dy
                if c[y2][x2] == "." and m[y2-2][x2-2] == inf:
                    m[y2-2][x2-2] = s
                    if (y2,x2) == (dh+1,dw+1):
                        notgoal = False
                        break
                    else:
                        yx2.append([y2, x2])
                        continue
                    break
            for dy, dx in warps:
                x2 = x+dx
                y2 = y+dy
                if c[y2][x2] == "." and m[y2-2][x2-2] == inf:
                    yxwarp.append([y2, x2])
        yx = yx2
    s += 1
    yx = yxwarp
    
if notgoal:
    print("-1")
else:
    print(s-1)

### FIXED CODE ###

from collections import deque

Y, X = map(int,input().split())
sy, sx = map(int, input().split())
gy, gx = map(int, input().split())
s = [["#"]*(X+4) for _ in range(Y+4)]
for y in range(Y):
    s[y+2] = *list("##"+input()+"##"), 
inf = Y*X
c = [[inf]*X for _ in range(Y)]
c[sy-1][sx-1] = 0
drs = [[-1,0],[1,0],[0,0],[0,-1],[0,1]]
dr2s = [[-2,-2],[-2,-1],[-2,0],[-2,1],[-2,2],
        [2,-2],[2,-1],[2,0],[2,1],[2,2],
        [-1,-2],[0,-2],[1,-2],
        [-1,2],[0,2],[1,2],
        [-1,-1],[-1,1],[1,-1],[1,1]
       ]

q = deque([(sy-1, sx-1)])

while q:
    y,x = q.popleft()
    cost = c[y][x]
    for dx,dy in drs:
        x2 = x+dx
        y2 = y+dy
        if s[y2+2][x2+2] == "." and c[y2][x2] > cost:
            c[y2][x2] = cost
            q.appendleft((y2,x2))
    for dy, dx in dr2s:
        x2 = x+dx
        y2 = y+dy
        if s[y2+2][x2+2] == "." and c[y2][x2] > cost+1:
            c[y2][x2] = cost+1
            q.append((y2,x2))
    
if c[gy-1][gx-1] == inf:
    print("-1")
else:
    print(c[gy-1][gx-1])
### BUGGY CODE ###

H, W, N = map(int,input().split())
ans = [0 for _ in range(10)]
points = []
MAP = [[0 for i in range(W)] for j in range(H)]
for _ in range(N):
    a, b = map(int,input().split())
    MAP[a-1][b-1] = 1
    points.append([a-1, b-1])
for i in range(N):
    a, b = points[i]
    for p in range(-1, 2):
        for q in range(-1, 2):
            cnt = 0
            if 1 <= a + p <= H - 2 and 1 <= b + q <= W - 2:
                for h in range(-1, 2):
                    cnt += sum(MAP[a + p + h][b + q - 1: b + q + 2])
            ans[cnt] += 1
for i in range(1,10):
    ans[i] //= i
SUM = sum(ans[1:])
ans[0] = (H-2) * (W-2) - SUM
for i in ans:
    print(i)

### FIXED CODE ###

from collections import defaultdict

H, W, N = map(int,input().split())
d = defaultdict(int)
ta = [1,1,0,-1,-1,-1,0,1,0]
tb = [0,1,1,1,0,-1,-1,-1,0]

for _ in range(N):
    a, b = map(int,input().split())
    for i in range(9):
        na = a + ta[i]
        nb = b + tb[i]
        if 1 <= na <= H and 1 <= nb <= W:
            d[(na, nb)] += 1

ans = [0] * 10

for i, v in d.items():
    if 2 <= i[0] <= H-1 and 2 <= i[1] <= W-1:
        ans[v] += 1

ans[0] = (H-2) * (W-2) - sum(ans)

for i in range(10):
    print(ans[i])
### BUGGY CODE ###

L, R = list(map(int, input().split()))

min_mod = 2018
for key, i in enumerate(range(L, R+1)):
    t = R - key
    for j in range(L, t):
        mod = (t * j) % 2019
        if mod < min_mod:
            min_mod = mod

print(min_mod)

### FIXED CODE ###

l, r = list(map(int,input().split()))

if r - l >= 2019:
  print(0)
  
else:
  ll = l % 2019
  rr = r % 2019
  if rr < ll:
    print(0)
    
  else:
    keep = 2020
    for i in range(ll, rr+1):
      for j in range(i+1, rr+1):
        keep = min((i*j)%2019, keep)
    print(keep)
### BUGGY CODE ###

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)
ls = []
for i in range(X):
    ls.append(p[i])
for i in range(Y):
    ls.append(q[i])
while(r != []):
    minimum = min(ls)
    
    if r[0] > minimum:
        ls[ls.index(minimum)] = r[0]
    r.pop(0)
print(sum(ls))

### FIXED CODE ###

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)
ls = []
for i in range(X):
    ls.append(p[i])
for i in range(Y):
    ls.append(q[i])
ls.extend(r)
ls.sort(reverse = True)
print(sum(ls[:X+Y]))
### BUGGY CODE ###

n = int(input())
data = list(map(int, input().split()))
 
a = 1
i = 0
j = 0
    
while i<n:
  while j < n:
    if data[j]==0:
    	  a = 0
    	  break
    j +=1
  # break
  a = a*data[i]
  i += 1
 
if a > 10**18:
  a = -1
 
print(a)

### FIXED CODE ###

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

flag = 0
if data.count(0) > 0:
	print(0)
else:
  a = data[0]
  for i in range(1,n):
    a = a*data[i]
    if a > 10**18:
      flag = 1
      break
      
  if flag == 1:
    print('-1')
  else:
    print(a)
### BUGGY CODE ###

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

mod = 10**9 + 7
for i in range(n):
  a[i] = a[i] % mod

ans = 0
for i in range(n):
  for j in range(i+1,n):
    ans += (a[i]) * (a[j])
print(ans % mod)

### FIXED CODE ###

n = int(input())
a = tuple(map(int,input().split()))
t = 0
mod = 10**9 + 7
at = []
for j in range(1,n):
  t += a[n-j] % mod 
  at.append(t)

ans = 0
for i in range(n-1):
  ans += at[n-2-i] *a[i]
print(ans % mod)
### BUGGY CODE ###

import itertools

N,C=map(int,input().split())

D=[None]*C
for i in range(C):
    D[i]=list(map(int,input().split()))

c=[None]*N
for i in range(N):
    c[i]=list(map(int,input().split()))

c_0=[]
c_1=[]
c_2=[]

for i in range(N):
    for j in range(N):
        if (i+j+2)%3==0:
            c_0+=[c[i][j]]
        elif (i+j+2)%3==1:
            c_1+=[c[i][j]]
        else:
            c_2+=[c[i][j]]

L=[]
for i in range(C):
    L+=[i+1]

ROOP=list(itertools.permutations(L,3))


ANS=float("inf")


for i in range(len(ROOP)):
    IWA=0
    for j in c_0:
        IWA+=D[j-1][ROOP[i][0]-1]
    for j in c_1:
        IWA+=D[j-1][ROOP[i][1]-1]
    for j in c_2:
        IWA+=D[j-1][ROOP[i][2]-1]
    if ANS>IWA:
        ANS=IWA

print(ANS)
    

### FIXED CODE ###

import itertools

N,C=map(int,input().split())

D=[None]*C
for i in range(C):
    D[i]=list(map(int,input().split()))

c=[None]*N
for i in range(N):
    c[i]=list(map(int,input().split()))

c_0=[]
c_1=[]
c_2=[]

for i in range(N):
    for j in range(N):
        if (i+j+2)%3==0:
            c_0+=[c[i][j]]
        elif (i+j+2)%3==1:
            c_1+=[c[i][j]]
        else:
            c_2+=[c[i][j]]

clist_0=[0]*(C+1)
clist_1=[0]*(C+1)
clist_2=[0]*(C+1)

for i in c_0:
    clist_0[i]+=1
for i in c_1:
    clist_1[i]+=1    
for i in c_2:
    clist_2[i]+=1



L=[]
for i in range(C):
    L+=[i+1]

ROOP=list(itertools.permutations(L,3))


ANS=float("inf")


for i in range(len(ROOP)):
    IWA=0
    for j in range(1,C+1):
        IWA+=D[j-1][ROOP[i][0]-1]*clist_0[j]+D[j-1][ROOP[i][1]-1]*clist_1[j]+D[j-1][ROOP[i][2]-1]*clist_2[j]
        
    if ANS>IWA:
        ANS=IWA

print(ANS)
### BUGGY CODE ###

import heapq
inf=10**9


def Prim():
    D=[inf for _ in range(n+1)]
    color=["white" for _ in range(n+1)]

    D[1]=0
    queue=[[0,1]]
    heapq.heapify(queue)
    while len(queue)>0:
        u=heapq.heappop(queue)[1]
        color[u]="black"

        for i,j in M[u]:
            if i<D[j] and color[j]!="black":
                D[j]=i
                color[j]="gray"
                heapq.heappush(queue,[i,j])

    return D


n=int(input())
XY=[list(map(int,input().split()))+[i+1] for i in range(n)]
M=[[] for _ in range(n+1)]
A,B=sorted(XY),sorted(XY,key=lambda x:x[1])

for i in range(n):
    if i!=0:
        M[A[i][2]].append([min(abs(A[i-1][0]-A[i][0]),abs(A[i-1][1]-A[i][1])),A[i-1][2]])
        M[B[i][2]].append([min(abs(B[i][0] - B[i - 1][0]), abs(B[i][1] - B[i - 1][1])), B[i - 1][2]])
    if i!=n-1:
        M[A[i][2]].append([min(abs(A[i+1][0]-A[i][0]),abs(A[i+1][1]-A[i][1])),A[i+1][2]])
        M[B[i][2]].append([min(abs(B[i][0]-B[i+1][0]),abs(B[i][1]-B[i+1][1])),B[i+1][2]])

print(sum(Prim()[1:]))

### FIXED CODE ###

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

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

    def Unite(self,x,y):
        x=self.Find_Root(x)
        y=self.Find_Root(y)

        if x==y:return
        elif self.rank[x]>self.rank[y]:
            self.root[x] +=self.root[y]
            self.root[y]=x
        else:
            self.root[y] +=self.root[x]
            self.root[x]=y
            if self.rank[x]==self.rank[y]:
                self.rank[y] +=1

    def isSameGroup(self,x,y):
        return self.Find_Root(x)==self.Find_Root(y)

    def size(self,x):
        return -self.root[self.Find_Root(x)]

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

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

    def group_members(self):
        return {i:self.members(i) for i in self.roots()}

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


def kruskal():
    D,M=[],[]

    A,B=sorted(XY),sorted(XY, key=lambda x: x[1])
    for i in range(n-1):
        M.append([min(abs(A[i][0]-A[i+1][0]),abs(A[i][1]-A[i+1][1])),A[i][2],A[i+1][2]])
        M.append([min(abs(B[i][0]-B[i+1][0]),abs(B[i][1]-B[i+1][1])),B[i][2],B[i+1][2]])

    M.sort()
    for w,s,t in M:
        if UnionFind.isSameGroup(s,t) is False:
            UnionFind.Unite(s,t)
            D.append(w)

    return D


import sys
input=sys.stdin.readline

n=int(input())
XY=[list(map(int,input().split()))+[i+1] for i in range(n)]
UnionFind=UnionFind(n+1)
print(sum(kruskal()))