Harryxun/stf_run huggingface dataset:
# coding=utf-8
from math import floor, ceil, sqrt, factorial, log, gcd, log10
from itertools import accumulate, permutations, combinations, product, combinations_with_replacement, chain
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from heapq import heappop, heappush, heappushpop, heapify
from copy import copy, deepcopy
import sys
INF = float('inf')
mod = 10**9+7
sys.setrecursionlimit(10 ** 8)


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

# 1 2 3
# a, b, c = LI()


def LI(): return list(map(int, sys.stdin.buffer.readline().split()))

# a = I()


def I(): return int(sys.stdin.buffer.readline())

# abc def
# a, b = LS()


def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()

# a = S()


def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')

# 2
# 1
# 2
# [1, 2]


def IR(n): return [I() for i in range(n)]

# 2
# 1 2 3
# 4 5 6
# [[1,2,3], [4,5,6]]


def LIR(n): return [LI() for i in range(n)]

# 2
# abc
# def
# [abc, def]


def SR(n): return [S() for i in range(n)]

# 2
# abc def
# ghi jkl
# [[abc,def], [ghi,jkl]]


def LSR(n): return [LS() for i in range(n)]

# 2
# abcd
# efgh
# [[a,b,c,d], [e,f,g,h]]


def SRL(n): return [list(S()) for i in range(n)]


# https://qiita.com/greenteabiscuit/items/31c46ab3ceac07e749d2


n, m = LI()
graph = [[] for _ in range(m)]
v = [False] * n

for i in range(m):
    a, b = LI()
    graph[a - 1].append(b)
    graph[b - 1].append(a)


def dfs(now, depth):
    if v[now - 1]:
        return 0
    if depth == n-1:
        return 1
    ans = 0
    v[now - 1] = True
    if len(graph[now - 1]) > 0:
        for node in graph[now - 1]:
            ans += dfs(node, depth+1)

    v[now - 1] = False
    # ここが大事.上で探索の一つが終わるからリセットしている.

    return ans


print(dfs(1, 0))


### FIXED CODE ###

#Nはノード数、Mはエッジ数
N, M = map(int, input().split())

path_matrix = []

#無向行列の初期化
for n in range(N):
    path_matrix.append([False] * N)

#まず無向グラフを表す行列を作る
for m in range(M):
    line = map(int, input().split())
    paths = [x - 1 for x in line]
    path_matrix[paths[0]][paths[1]] = True
    path_matrix[paths[1]][paths[0]] = True

#最初はvisited、つまり訪問したことのあるノードはひとつもないので、Falseにする
visited = [False] * N


#再帰関数、ここが難しいかもしれない。
def dfs(now, depth):
    if visited[now]:
        return 0
    if depth == N - 1:
        return 1

    visited[now] = True
    total_paths = 0
    for i in range(0, N):
        if path_matrix[now][i]:
            total_paths += dfs(i, depth + 1)

    visited[now] = False

    return total_paths

print(dfs(0, 0))
while True:
    a,b=map(int,input().split() )
    for i in range(a):
        for j in range(b):
            print('#',end='')
        print()
    if(a==0 and b==0):
        break;



### FIXED CODE ###


while True:
    a,b= map(int,input().split())
    if(a==0 and b==0):
        break;
    for i in range(a):
            print('#'*b)
    print()
import math

A,B,C,D=map(int,input().split())

#最小公倍数a * b / gcd(a, b)
kobaisu=C/math.gcd(C,D)*D



ans=B -(B//C+B//D-B//kobaisu)

ans2=(A-1)-((A-1)//C +(A-1)//D - (A-1)//kobaisu)

print(int(ans-ans2))

### FIXED CODE ###

import fractions

A,B,C,D=map(int,input().split())


kobaisu=C//fractions.gcd(C,D)*D


ans=B -(B//C+B//D-B//kobaisu)

ans2=(A-1)-((A-1)//C +(A-1)//D - (A-1)//kobaisu)

print(int(ans-ans2))
N,K=map(int,input().split())
l=[False for _ in range(N)]
for i in range(K):
    d=int(input())
    A=list(map(int,input().split()))
    for j in range(N):
        if l[j]==A[j]:
            l[j]=True
print(list.count(False)) 

### FIXED CODE ###

N,K=map(int,input().split())
l=[False for x in range(N)]
for i in range(K):
    d=int(input())
    A=list(map(int,input().split()))
    for j in A:
        l[j-1]=True
print(l.count(False))
N = int(input())
ans = 0
f = False
xx = 0
xA = 0
Bx = 0
BA = 0
A = 0
B = 0
for i in range(N):
  S = input()
  for i in range(len(S) - 1):
    if S[i] == "A" and S[i + 1] == "B":
      ans += 1
  if S[0] == "B":
    if S[-1] == "A":BA += 1
    else:Bx += 1
  else:
    if S[-1] == "A":xA += 1
if xA != 0 and Bx != 0:
  ans += BA + 1
  xA -= 1
  Bx -= 1
elif xA != 0:
  ans += BA
  xA -= 1
elif Bx != 0:
  ans += AB
  Bx -= 1
else:
  if BA >= 2:
    ans += BA - 1
ans += min(Bx,xA)
print(ans)


### FIXED CODE ###

N = int(input())
ans = 0
f = False
xx = 0
xA = 0
Bx = 0
BA = 0
A = 0
B = 0
for i in range(N):
  S = input()
  for i in range(len(S) - 1):
    if S[i] == "A" and S[i + 1] == "B":
      ans += 1
  if S[0] == "B":
    if S[-1] == "A":BA += 1
    else:Bx += 1
  else:
    if S[-1] == "A":xA += 1
if xA != 0 and Bx != 0:
  ans += BA + 1
  xA -= 1
  Bx -= 1
elif xA != 0:
  ans += BA
  xA -= 1
elif Bx != 0:
  ans += BA
  Bx -= 1
else:
  if BA >= 2:
    ans += BA - 1
ans += min(Bx,xA)
print(ans)
N = int(input())
A = int(inpiut())

print(N**2-A)

### FIXED CODE ###

N = int(input())
A = int(input())
 
print(N**2-A)
# ABC068C - Cat Snuke and a Voyage (ARC079C)
def main():
    N, M = tuple(map(int, input().split()))
    A = tuple(tuple(map(int, input().split())) for _ in range(M))
    S, D = set(), set()  # start, destination
    for a, b in E:
        if a == 1:
            S |= {b}
        if b == N:
            D |= {a}
    flg = S & D  # if the same X in both S & D -> possible
    print("POSSIBLE" if flg else "IMPOSSIBLE")


if __name__ == "__main__":
    main()

### FIXED CODE ###

# ARC079C - Cat Snuke and a Voyage (ABC068C)
import sys
input = sys.stdin.readline

def main():
    n, m = list(map(int, input().rstrip().split()))
    s, d = set(), set()
    for _ in range(m):
        a, b = list(map(int, input().rstrip().split()))
        if a == 1:
            s.add(b)
        elif b == n:
            d.add(a)
    ans = len(s & d)
    print("POSSIBLE" if ans else "IMPOSSIBLE")


if __name__ == "__main__":
    main()
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
N = map(int, input().split())
# N=3
# M=1
# A=[5,4,2,1]

cnt = 0
cnt1 = 0

# for i in range(N):
#     cnt = cnt + A[i]
#
# for i in range(N):
#     if A[i] / cnt > 1 / 4 / M:
#         cnt1 = cnt1 + 1

sum=0
for i in range(1,N+1):
    for j in range(1,N+1):
        for k in range(1,N + 1):
            sum =sum + gcd(i,j,k)
            # print(i,j,k)

# a1 = N % 10
# a2 = ((N-a1*10) % 100)/10
# a3 = ((N-a1*100-a2*10))/100

print(sum)


### FIXED CODE ###

import math
from functools import reduce
 
def gcd(*numbers):
    return reduce(math.gcd, numbers)
N = int(input())
# N=3
# M=1
# A=[5,4,2,1]
 
cnt = 0
cnt1 = 0
 
# for i in range(N):
#     cnt = cnt + A[i]
#
# for i in range(N):
#     if A[i] / cnt > 1 / 4 / M:
#         cnt1 = cnt1 + 1
 
sum=0
for i in range(1,N+1):
    for j in range(1,N+1):
        for k in range(1,N + 1):
            sum =sum + gcd(i,j,k)
            # print(i,j,k)
 
# a1 = N % 10
# a2 = ((N-a1*10) % 100)/10
# a3 = ((N-a1*100-a2*10))/100
 
print(sum)
n, k = map(int, input().split())
    p = list(map(int, input().split()))
    maxI = 0
    maxT = 0
    t = 0
    for i in range(n):
        t += p[i]
        if k <= i:
            t -= p[i-k]
        if maxT < t:
            maxT = t
            maxI = i
    ans = 0
     
     
    def calc(n):
        ans = 0
        for i in range(1, n+1):
            ans += i/n
        return ans
     
     
    for i in range(maxI-k+1, maxI+1):
        #print(i, p[i])
        ans += calc(p[i])
    # print(p)
    #print(maxT, maxI)
    print(ans)

### FIXED CODE ###

# ABC154d


def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    # 再帰関数を使わない限りPypyで出すこと

    n, k = map(int, input().split())
    p = list(map(int, input().split()))
    ans = 0
    pos = 0

    def cum(array):
        result = [0]
        for i in range(len(array)):
            result.append(array[i]+result[i])
        return result

    def calc(N):
        t = 0
        for i in range(1, N+1):
            t += i / N
        return t

    p2 = cum(p)

    for i in range(n - k+1):
        if ans < p2[i+k]-p2[i]:
            ans = p2[i+k]-p2[i]
            pos = i

    ans = 0
    for i in range(pos, pos + k):
        ans += calc(p[i])
    print(ans)


if __name__ == '__main__':
    main()
n, x = map(int, input().split())
l = [int(i) for i in input().split()]

i = 0
d = 0
c = 0
while d <= x:
    c += 1
    d += l[i-1]
    i += 1
    
print(c)

### FIXED CODE ###

N, X = map(int, input().split())
L = [int(i) for i in input().split()]

count = 1
d = 0
for l in L:
    d += l
    if d > X:
        break
    count += 1
    
print(count)
S,T = open(0)

if T.startswith(S) = True:
  print("Yes")
  
else:
  primt("No")


### FIXED CODE ###

S = str(input())
T = str(input())

if T.startswith(S):
  print("Yes")
else:
  print("No")
a, b, k = map(int, input().split())
li =[]
for i in range(1, min(a, b)):
    elif a % i == 0 and b % i == 0:
        li.append(i)

print(li[-k])

### FIXED CODE ###

a, b, k = map(int, input().split())

def ooo(x):
    lix =[]
    for i in range(1, x+1):
        if x % i == 0:
            lix.append(i)
    return lix

lia = ooo(a)
lib = ooo(b)
li = lia + lib

li2 =[]
for n in range(len(li)):
    if not li[n] in li2: 
        if li.count(li[n]) == 2:
            li2.append(li[n])

li2.sort(reverse=True)
print(li2[k-1])
import math
n=int(input())
t=[int(input()) for i in range(n)]
ans=t[0]
for i in range(1,n):
    ans=ans*t[i]//math.gcd(ans,t[i])
print(ans)

### FIXED CODE ###

import fractions
n=int(input())
t=[int(input()) for i in range(n)]
ans=t[0]
for i in range(1,n):
    ans=ans*t[i]//fractions.gcd(ans,t[i])
print(ans)
N = int(input())

mi = 10**10

for a in range(N):
  for b in range(a):
    if a*b = N:
      if mi < a*b-2:
        mi == a*b-2
print(mi)
      

### FIXED CODE ###

import math

N = int(input())
 
mi = 10**100

for a in range(1, int(math.sqrt(N))+1, 1):
  if N % a == 0:  
    b = N//a
    if mi > a+b-2:
      mi = a+b -2
print(mi)
while True:
    h,a = map(int, input().split())
    x = h // a
    y = h / a
    if x == y:
        print(x)
    else:
        print(x + 1)


### FIXED CODE ###

h,a = map(int, input().split())
x = h // a
y = h / a
if x == y:
    print(x)
else:
    print(x + 1)
lis = list(map(int, input().split()))
lis2 = list(map(int, input().split()))
n = lis[0]
k = lis[1]
d = {}

for i in range(n):
    if lis2[i] not in d.keys():
       d[lis2[i]] = 1
    else:
        d[lis2[i]] += 1 
        
if len(b) <= k:
    print(0)
else:
    a = list(d.values())
    a.sort()
    a.reverse()
    swap = 0
    for i in range(k,len(a)):
        swap += a[i]
    print(swap)

### FIXED CODE ###

lis = list(map(int, input().split()))
lis2 = input().split()
n = lis[0]
k = lis[1]
d = {}

for i in range(n):
    if lis2[i] not in d.keys():
       d[lis2[i]] = 1
    else:
        d[lis2[i]] += 1 
        
if len(list(d.values())) <= k:
    print(0)
else:
    a = list(d.values())
    a.sort()
    a.reverse()
    swap = 0
    for i in range(k,len(a)):
        swap += a[i]
    print(swap)
n = int(raw_input())

occ = {}
for i in map(int, raw_input().split()):
    if not i in occ:
        occ[i] = 0

    occ[i] += 1

found = 0
rect = 1

for i in sorted(occ, reverse=True):
    if occ[i] >= 4 and found < 1:
        found += 2
        rect *= i*i
    elif occ[i] => 2:
        found += 1
        rect *= i

    if found == 2:
        print rect
        break
    
if found < 2:
    print 0
        
    


### FIXED CODE ###

n = int(raw_input())

occ = {}
for i in map(int, raw_input().split()):
    if not i in occ:
        occ[i] = 0

    occ[i] += 1

found = 0
rect = 1

for i in sorted(occ, reverse=True):
    if occ[i] >= 4 and found < 1:
        found += 2
        rect *= i*i
    elif occ[i] >= 2:
        found += 1
        rect *= i

    if found == 2:
        print rect
        break
    
if found < 2:
    print 0
from collections import deque

N,K=map(int,input().split())
A=list(map(int,input().split()))
A.sort(key=lambda x:abs(x),reverse=True)
MOD=10**9+7

P=[]
M=[]
for num in A:
    if num>=0:
        P.append(num)
    else:
        M.append(num)
       
#print(P,M)
       
if len(M)==N and K%2==1:
    ans= M[-1]
    for j in range(1,K):
        ans = (ans*M[-1-j])%MOD
       
else:
    Q=deque(P)
    R=deque(M)
    cnt=0
    ans_list=[]

    while cnt<K:

        if Q:
            tmp_p=Q.popleft()
        else:
            tmp_p=0

        if len(R)>=2 and K-cnt>=2:
            x= R.popleft()
            y= R.popleft()
            tmp_q= x*y
        else:
            tmp_q=0

        if tmp_p>=tmp_q:
            ans_list.append(tmp_p)
            if tmp_q>0:
                R.appendleft(y)
                R.appendleft(x)
            cnt +=1
        else:
            ans_list.append(x)
            ans_list.append(y)
            Q.appendleft(tmp_p)
            cnt +=2

    #print(cnt, tmp_p, tmp_q,Q,R,ans_list)
       
    ans=ans_list[0]
    for j in range(1,len(ans_list)):
        ans = (ans*ans_list[j])%MOD
       
       
print(ans)


### FIXED CODE ###

N=int(input())
if N%1000==0:
    print(0)
else:
    print(1000-N%1000)
N = int(input())
D = list(map(int, input().split()))

diff = [0]*13
# D.sort()
for i in range(N):
    diff[D[i]] += 1
    if diff[i] > 2:
        print(0)
        exit(0)
if diff[0] > 0:
    print(0)
    exit(0)
else:
    diff2 = [diff[12 - i] for i in range(1, N-1)]

ans = [0]
for i in range(N):
    if i % 2 == 0:
        ans.append(D[i])
    else:
        ans.append(24 - D[i])

ans = [abs(ans[i+1] - ans[i]) for i in range(N-1)]

print(min(ans))


### FIXED CODE ###

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


D.sort()
ans = [0]
ans = [D[i]
       for i in range(N) if i % 2 == 0] + [24 - D[i] for i in reversed(range(N)) if i % 2 == 1]

ans = [0] + ans + [0]

ans = [abs(ans[i+1] - ans[i]) for i in range(N+1)]

print(min(ans))
r, g, b = input().split()
int(r+g+b)
if n % 4:
  print("NO")
else:
  print("YES")

### FIXED CODE ###

r, g, b = input().split()
n = int(r+g+b)
if n % 4:
  print("NO")
else:
  print("YES")
n, m = list(map(int, input().split()))

xs = list(map(int, input().split()))
ys = list(map(int, input().split()))

MODULO = 10**9 +7

def MSum(x, y):
    return (x + y) % MODULO

def MPro(x, y):
    return (x * y) % MODULO

def foo(zs, k):
    s = 0
    for i in range(k):
       s = MSum(s, MPro(MPro(zs[i], k-i), i+1))
    return s

def diff(zs, k):
    previous = None
    rs = [0] * (k-1)
    for i, z in enumerate(zs):
        if i == 0:
            previous = z
            continue
        rs[i-1] = z - previous
        previous = z
    return rs

dx = diff(xs, n)
dy = diff(ys, n)

print(MPro(foo(dx, n-1), foo(dy, m-1)))



### FIXED CODE ###

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

xs = list(map(int, input().split()))
ys = list(map(int, input().split()))

MODULO = 10**9 +7

def MSum(x, y):
    return (x + y) % MODULO

def MPro(x, y):
    return (x * y) % MODULO

def foo(zs, k):
    s = 0
    for i in range(k):
       s = MSum(s, MPro(MPro(zs[i], k-i), i+1))
    return s

def diff(zs, k):
    previous = None
    rs = [0] * (k-1)
    for i, z in enumerate(zs):
        if i == 0:
            previous = z
            continue
        rs[i-1] = z - previous
        previous = z
    return rs

dx = diff(xs, n)
dy = diff(ys, m)

print(MPro(foo(dx, n-1), foo(dy, m-1)))
M,N = [int(x) for x in input()]
print(M+N if M==N else 2*max(M,N)-1)

### FIXED CODE ###

M,N = [int(x) for x in input().split()]
print(M+N if M==N else 2*max(M,N)-1)
n=int(input())
x=[0]*n
y=[0]*n
for i in range(n):
    x[i],y[i]=map(int,input().split())

from collections import Counter
cnt = Counter()
    
for i in range(n):
    for j in range(i+1,n):
        cnt[str((x[j]-x[i],y[j]-y[i]))]+=1

print(n-cnt.most_common()[0][1])


### FIXED CODE ###

n=int(input())
if n==1:
    print(1)
    exit()
x=[0]*n
y=[0]*n
for i in range(n):
    x[i],y[i]=map(int,input().split())

from collections import Counter
cnt = Counter()
    
for i in range(n):
    for j in range(n):
        if i!=j:
            cnt[str((x[j]-x[i],y[j]-y[i]))]+=1

print(n-cnt.most_common()[0][1])
N = int(input())
A = list(map(int, input().split()))

# 解法メモ
# ひとつでも山の雨の量がわかれば全て求まる
# 3つの山があり、1つめの山の値をxとしたとき
# 1つめ: x
# 2つめ：A1 - x
# 3つめ：A2 - (A1 - x)
# 1つめ：A3 - (A2 - (A1 - x))
# 1つめ２つを式にする
# x = A3 - (A2 - (A1 - x))
# x = A3 - A2 + A1 - x
# 2x = A3 - A2 + A1
summary = 0

for i in range(N):
    if i % 2 == 0:
        summary += A[i]
    else:
        summary -= A[i]

for i in range(N):
    print(summary)
    summary = (A[i] - (summary//2)) * 2


### FIXED CODE ###

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

if A * N < B:
    B = A * N

print(B)
import math
n = int(input())
a = list(map(int,input().split()))
ans = [0]*(n-1)
ans[0] = math.gcd(a[0],a[1])
for i in range(1,n-1):
  ans[i] = math.gcd(ans[i-1],a[i+1])
print(ans[n-2])

### FIXED CODE ###

import fractions
n = int(input())
a = list(map(int,input().split()))
ans = [0]*(n-1)
ans[0] = fractions.gcd(a[0],a[1])
for i in range(1,n-1):
  ans[i] = fractions.gcd(ans[i-1],a[i+1])
print(ans[n-2])
n = int(input())
a = list(map(int,input().split()))
#n = 3
#a = [1,0,1]
if a.count(0) == n:
    print("Yes")
else:
  if n % 3 != 0:
    print("No")
  else:
    s = set(a)
    if len(s)>3:
      print("No")
    elif len(s)==2:
      if a.count(0)==n/3:
        print("Yes")
      else:
        print("No")
    else:
      l = list(s)
      if (l[0]^l[1] == l[2] and l[1]^l[2] == l[0] and l[2]^l[0] == l[1]) and (a.count(l[0])==n/3 and a.count(l[1])==n/3):
        print("Yes")
      else:
        print("No")

### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
if a.count(0) == n:
    print("Yes")
else:
  if n % 3 != 0:
    print("No")
  else:
    s = set(a)
    if len(s)>3 or len(s)==1:
      print("No")
    elif len(s)==2:
      if a.count(0) == n/3:
        print("Yes")
      else:
        print("No")
    else:
      l = list(s)
      if (l[0]^l[1] == l[2] and l[1]^l[2] == l[0] and l[2]^l[0] == l[1]) and (a.count(l[0])==n/3 and a.count(l[1])==n/3):
        print("Yes")
      else:
        print("No")
N=int(input())
D=list(map(int, input().split()))
M=int(input())
T=list(map(int, input().split()))
if N<M:
  print('NO')
  exit()
A={}
B={}
for i in D:
  if i not in A:
    A[i]=0
  A[i]+=1
  
for i in T:
  if i not in B:
    B[i]=0
  B[i]+=1
  
for i in B.keys():
  if not A[i]:
    print('NO')
    exit()
  if A[i]<B[i]:
    print('NO')
    exit()
print('YES')

### FIXED CODE ###

N=int(input())
D=list(map(int, input().split()))
M=int(input())
T=list(map(int, input().split()))
if N<M:
  print('NO')
  exit()
  
A={}
B={}
for i in D:
  if i not in A:
    A[i]=0
  A[i]+=1
  
for i in T:
  if i not in B:
    B[i]=0
  B[i]+=1
  
for i in B.keys():
  if not i in A:
    print('NO')
    exit()
  if A[i]<B[i]:
    print('NO')
    exit()
print('YES')
a,p=map(int,input().split())
pritn((3a+p)//2)

### FIXED CODE ###

a,p=map(int, input().split())
print((3*a+p)//2)
S=input()
T=input()
j=0
i=-1
flag = False
while i>=-1*len(S)-1+len(T):
    while j>=-1*len(T)-1:
        if i<=-1*len(S)-1+len(T):
            A="UNRESTORABLE"
            flag=True
            break
        elif j==-1*len(T):
            if i!=-1:
                P=S.replace(S[i-len(T):i+1],T,1)
                A=P.replace("?","a")
                i=-1*len(S)-1+len(T)
                flag=True
                break
            else:
                P=S.replace(S[i-len(T):],T,1)
                A=P.replace("?","a")
                flag=True
                break
        elif S[i+j]==T[j-1] or S[i+j]=="?":
            j=j-1
        else:
            j=0
            i=i-1
    if flag:
        break
print(A)

### FIXED CODE ###

S1=input()
T1=input()
S=""
T=""
A=""
for i in range(1,len(S1)+1):
    S+=S1[-i]
for i in range(1,len(T1)+1):
    T+=T1[-i]
j=0
i=0
flag = False
while i<=len(S)+1-len(T):
    while j<=len(T)+1:
        if i==len(S)-len(T)+1:
            A="UNRESTORABLE"
            flag=True
            break
        elif j==len(T):
            P=S.replace(S[i:i+len(T)],T,1)
            Q=P.replace("?","a")
            for i in range(1,len(Q)+1):
                A+=Q[-i]
            flag=True
            break
        elif S[i+j]==T[j] or S[i+j]=="?":
            j+=1
        else:
            j=0
            i+=1
    if flag:
        break
print(A)
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def YesNo(x): return 'Yes' if x else 'No'
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()
from collections import defaultdict

def main():
    N = II()
    xy = []
    for _ in range(N):
        xy.append(LI())
    x2g = defaultdict(int)
    y2g = defaultdict(int)
    g2y = defaultdict(set)
    g2x = defaultdict(set)
    for x, y in xy:
        gx = x2g[x]
        gy = y2g[y]
        if gx > 0 and gy > 0:  # merge
            if len(g2x[gx]) + len(g2y[gx]) < len(g2x[gy]) + len(g2x[gy]):
                gx, gy = gy, gx
            for x in g2x[gy]:
                x2g[x] = gx
            for y in g2y[gy]:
                y2g[y] = gx
            g2y[gx] += g2y[gy]
            g2x[gx] += g2x[gy]
            g2y[gy] = set()
            g2x[gy] = set()
        elif gx > 0:
            y2g[y] = gx
            g2y[gx].add(y)
        elif gy > 0:
            x2g[x] = gy
            g2x[gy].add(x)
        else:
            x2g[x] = x
            y2g[y] = x
            g2x[x].add(x)
            g2y[x].add(y)
    ans = -N
    for g in g2x.keys():
        if g2x[g]:
            ans += len(g2x[g]) * len(g2y[g])
    return ans

print(main())

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def YesNo(x): return 'Yes' if x else 'No'
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()
from collections import defaultdict

def main():
    N = II()
    xy = []
    for _ in range(N):
        xy.append(LI())
    xy2g = defaultdict(int)
    g2xy = defaultdict(set)
    for x, y in xy:
        y = -y
        gx = xy2g[x]
        gy = xy2g[y]
        if gx != 0 and gy != 0:  # merge
            if gx == gy:
                continue
            if len(g2xy[gx]) < len(g2xy[gy]):
                gx, gy = gy, gx
            for xy in g2xy[gy]:
                xy2g[xy] = gx
            g2xy[gx] |= g2xy[gy]
            g2xy[gy].clear()
        else:
            g = gx or gy or x
            xy2g[x] = xy2g[y] = g
            g2xy[g] |= {x, y}
    ans = -N
    for xys in g2xy.values():
        m, p = 0, 0
        for xy in xys:
            if xy < 0:
                m += 1
            else:
                p += 1
        ans += m * p
    return ans

print(main())
N,M = map(int,input().split())
res = []
for i in range(N):
    res.append(list(map(int,input().split())))

if N == 1:
    print(res[0][0])
    exit()

s = [0 for i in range(M)]
for i in res:
    for j in i[1:]:
        s[j-1] += 1

count = 0
for i in range(N):
    if s[i] == N:
        count += 1

print(count)

### FIXED CODE ###

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

if N == 1:
    print(res[0][0])
    exit()

s = [0 for i in range(M)]
for i in res:
    for j in i[1:]:
        #print(j)
        s[j-1] += 1

#print(s)

count = 0
for i in range(M):
    if s[i] == N:
        count += 1

print(count)
n, m = map(int, input().split())
 
S = set(range(1, m+1))
 
for i in range(n):
  K, *a = map(int, input().split())
  S = S & set(A)
 
print(len(S))

### FIXED CODE ###

n, m = map(int, input().split())

S = set(range(1, m + 1))

for i in range(n):
    K, *a = map(int, input().split())
    S = S & set(a)

print(len(S))