CodeEff/ECCO huggingface dataset:
# Original solution
N = int(eval(input()))
A = list(map(int,input().split()))
num = []
for i in range(1,N):
  num.append(abs(sum(A[:i]) - sum(A[i:])))
print((min(num)))


# Optimized solution
N = int(eval(input()))
A = list(map(int,input().split()))
X = sum(A)
x = 0
result = []
for i in range(N-1):
  x += A[i]
  result.append(abs(X - 2*x))
print((min(result)))
# Original solution
n = int(eval(input()))
a = [int(x) for x in input("").split()]

min = 0

for i in range(1,n):
    all = 0
    for j in range(0,i):
        all += a[j]

    for j in range(i,n):
        all -= a[j]

    if i == 1:
        min = abs(all)
    else:
        if min > abs(all):
            min = abs(all)

print(min)

# Optimized solution
n = int(eval(input()))
a = [int(x) for x in input("").split()]

min = 0

left = []
for i in range(n-1):
    left.append(a[i])
    if i > 0:
        left[i] += left[i-1]

a.reverse()

right = []
for i in range(n-1):
    right.append(a[i])
    if i > 0:
        right[i] += right[i-1]

right.reverse()

min = 0
for i in range(1,n):
    if i == 1:
        min = abs(left[i-1] - right[i-1])
    else:
        if min > abs(left[i-1] - right[i-1]):
            min = abs(left[i-1] - right[i-1])

print(min)
# Original solution
#!/usr/bin/env python3
import math

N = int(input().split()[0])
a_list = list(map(int, input().split()))

total = sum(a_list)
work_list = []

for i in range(N - 1):
    sunuke = sum(a_list[: i + 1])
    arai = total - sunuke
    work_list.append(abs(sunuke - arai))

ans = min(work_list)
print(ans)


# Optimized solution
#!/usr/bin/env python3
import math

N = int(input().split()[0])
a_list = list(map(int, input().split()))

total = sum(a_list)
work_list = []
sunuke = 0

for i in range(N - 1):
    sunuke += a_list[i]
    arai = total - sunuke
    work_list.append(abs(sunuke - arai))

ans = min(work_list)
print(ans)
# Original solution
n=int(eval(input()))
A=list(map(int,input().split()))
L=[A[0]]
R=[A[-1]]
for i in range(1,n-1):
    l=L[i-1]+A[i]
    L.append(l)
    r=R[i-1]+A[-(i+1)]
    R.append(r)
c=2*10**14
for i in range(n-1):
    s=abs(L[i]-R[-(i+1)])
    c=min(c,s)
print(c)

# Optimized solution
n=int(eval(input()))
A=list(map(int,input().split()))
X=sum(A)
x=0
c=2*10**14
for i in range(n-1):
    x+=A[i]
    s=abs(X-2*x)
    c=min(c,s)
print(c)
# Original solution
n=int(eval(input()))
l=list(map(int,input().split()))
m=float('inf')
for i in range(1,n):
    m=min(m,abs(sum(l[:i])-sum(l[i:])))
print(m)


# Optimized solution
n=int(eval(input()))
l=list(map(int,input().split()))
a,b=l[0],sum(l[1:])
m=abs(a-b)
for v in l[1:-1]:
    a+=v
    b-=v
    m=min(m,abs(a-b))
print(m)
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
s = [0] * (n + 1)

for i in range(n):
    s[i + 1] = s[i] + a[i]

ans = 3e9
for i in range(n - 1):
    ans = min(ans, abs((s[n] - s[i + 1]) - s[i + 1]))
print(ans)

# Optimized solution
N=int(eval(input()))
A=list(map(int,input().split()))
s=[0]*(N+1)
for i in range(N): s[i + 1]=s[i]+A[i]
C=[0]*(N)
C[0]=-s[N]
x=10**10
p=0
for i in range(1, N):
    C[i]=C[i-1]+2*A[i-1]
    if x>abs(C[i]):
        x=abs(C[i])
        p=i
print(x)
#print(C)
# Original solution
n=int(eval(input()))
a=list(map(int,input().split()))
A=a[0]
for i in range(n-1):
    if i==0:
        ans=abs(sum(a)-2*A)
    else:
        ans=min(abs(sum(a)-2*A),ans)
    A+=a[i+1]
print(ans)

# Optimized solution
n=int(eval(input()))
a=list(map(int,input().split()))
b=sum(a)
A=a[0]
for i in range(n-1):
    if i==0:
        ans=abs(b-2*A)
    else:
        ans=min(abs(b-2*A),ans)
    A+=a[i+1]
print(ans)
# Original solution
N = int(eval(input()))
L = list(map(int,input().split()))
S = sum(L)
A = []
for i in range(1,N):
  p = abs(2*sum(L[0:i])-S)
  A.append(p)
print((min(A)))


# Optimized solution
N = int(eval(input()))
L = list(map(int,input().split()))
S = sum(L)
k = 0
ans = abs(2*sum(L[0:1])-S)
for i in range(0,N-1):
  k += L[i]
  ans = min(ans,abs(2*k-S))
print(ans)
# Original solution
n=int(eval(input()))
a = list(map(int,input().split()))	
m=n*(10**9)
for i in range(n-1):
    if m > abs(sum(a[:i+1])-sum(a[i+1:])):
        m=abs(sum(a[:i+1])-sum(a[i+1:]))
print(m)





# Optimized solution
n=int(eval(input()))
a = list(map(int,input().split()))	
m=n*(10**9)
s=sum(a)
M=0
for i in range(n-1):
    M+=a[i]
    if abs(s-2*M) < m:
        m=abs(s-2*M)
print(m)
# Original solution
def solve():  # 068C - Splitting Pile
    N = int(eval(input()))
    a = [int(x) for x in input().split(" ")]
    X = sum(a)
    min_sum = 2 * 1e10

    for i in range(N - 1):
        if abs(X - 2 * sum(a[:i + 1])) < min_sum:
            min_sum = abs(X - 2 * sum(a[:i + 1]))
    print(min_sum)


solve()

# Optimized solution
def solve():  # 068C - Splitting Pile
    N = int(eval(input()))
    a = [int(x) for x in input().split(" ")]
    X = sum(a)
    sum_val = 0
    min_sum = 2 * 1e20

    for i in range(N-1):
        sum_val += a[i]
        y = X - sum_val
        min_sum = min(min_sum, abs(sum_val - y))

    print(min_sum)


solve()
# Original solution
def c_splitting_pile(N, A):
    # Aの要素の総和をX、カードの山の先頭i枚の総和をxとすると、
    # |y-x|=|X-2x|である。各i(1<=i<=N-1)についてこの値を計算すればよい
    from itertools import accumulate
    total = sum(A)
    return min([abs(total - 2 * x) for x in accumulate(A[:-1])])

N = int(eval(input()))
A = [int(i) for i in input().split()]
print((c_splitting_pile(N, A)))

# Optimized solution
def c_splitting_pile():
    # X = sum(A)、カードの山の先頭 i 枚の総和を x とすると、
    # |y-x| = |X-2x| である。各 i(1<=i<=N-1) についてこの値を計算すればよい
    from itertools import accumulate
    N = int(eval(input()))
    A = [int(i) for i in input().split()]
    total = sum(A)
    return min([abs(total - 2 * x) for x in accumulate(A[:-1])])

print((c_splitting_pile()))
# Original solution
import sys
from itertools import accumulate

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *A = list(map(int, read().split()))

    csum = [0]
    csum.extend(accumulate(A))

    ans = INF
    for i in range(1, N):
        tmp = abs(csum[i] - (csum[N] - csum[i]))
        if ans > tmp:
            ans = tmp

    print(ans)
    return


if __name__ == '__main__':
    main()


# Optimized solution
import sys
from itertools import accumulate

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *A = list(map(int, read().split()))

    total = sum(A)
    ans = INF
    s = 0
    for a in A[:-1]:
        s += a
        if ans > abs(total - 2 * s):
            ans = abs(total - 2 * s)

    print(ans)
    return


if __name__ == '__main__':
    main()
# Original solution
n=int(eval(input()))
a=list(map(int,input().split()))
s=sum(a)
sunuke=0
arai=0
ans=10**18
for i in range(1,n):
    sunuke=sum(a[:i])
    arai=s-sunuke
    t=abs(sunuke-arai)
    ans=min(t,ans)
print(ans)

# Optimized solution
from itertools import accumulate
n=int(eval(input()))
a=list(map(int,input().split()))
s=sum(a)
# a=[0]+a
acc=accumulate(a)
acc=list(acc)

sunuke=0
arai=0
ans=10**18
for i in range(1,n):
    sunuke=acc[i-1]
    arai=s-sunuke
    t=abs(sunuke-arai)
    ans=min(t,ans)
print(ans)
# Original solution
from itertools import accumulate
n=int(eval(input()))
a=list(map(int,input().split()))
s=sum(a)
# a=[0]+a
acc=accumulate(a)
acc=list(acc)

sunuke=0
arai=0
ans=10**18
for i in range(1,n):
    sunuke=acc[i-1]
    arai=s-sunuke
    t=abs(sunuke-arai)
    ans=min(t,ans)
print(ans)

# Optimized solution
n=int(eval(input()))
a=list(map(int,input().split()))
from itertools import accumulate
s=list(accumulate(a))
a_sum=sum(a)
ans=10**18
if n==2:
  print((abs(a[0]-a[1])))
  exit(0)
for i in range(1,n-1):
  x=s[i]
  y=a_sum-x
  ans=min(ans,abs(x-y))
print(ans)
# Original solution
n = int(eval(input()))
a = [int(x) for x in input().split()]

suma = sum(a)
x = a[0]
ans = abs(suma - x * 2)

for i in range(1, n - 1):
    x += a[i]
    ans = min(ans, abs(suma - x * 2))
else:
    print(ans)

# Optimized solution
n = int(eval(input()))
a = [int(x) for x in input().split()]

x = 0
ans = []
suma = sum(a)

for i in range(n - 1):
    x += a[i]
    ans.append(abs(suma - x * 2))
else:
    print((min(ans)))
# Original solution
def test(L):
    T = abs(sum(A[:L]) - sum(A[L:]))
    return T < answer


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

left = 0
right = N
answer = float("inf")

for i in range(1, N):
    if test(i):
        answer = abs(sum(A[:i]) - sum(A[i:]))

print(answer)


# Optimized solution
from itertools import accumulate

N = int(eval(input()))
A = list(map(int, input().split()))
A = list(accumulate(A))
answer = abs((A[-1] - A[0]) - A[0])
for i in range(N - 1):
    L = A[-1] - A[i]
    K = abs(L - A[i])
    if K < answer:
        answer = K


print(answer)
# Original solution
n = int(eval(input()))
A = [int(i) for i in input().split()]
sumA = sum(A)

tmp = 0
ans = 10 ** 10
for i in range(n - 1):
    tmp += A[i]
    ans = min(ans, abs(tmp -(sumA - tmp)))
print(ans)

# Optimized solution
N = int(eval(input()))
A = [int(i) for i in input().split()]

x = sum(A)
tmp = 0
ans = float('inf')
for i in range(N - 1):
    tmp += A[i]
    ans = min(ans, abs(tmp - (x - tmp)))
    
print(ans)
# Original solution
N = int(eval(input()))
a = list(map(int, input().split()))
ans = 10**10

for i in range(1, N):
    x = sum(a[:i])
    y = sum(a[i:])
    check = abs(x - y)
    if ans > check:
        ans = check

print(ans)

# Optimized solution
N = int(eval(input()))
a = list(map(int, input().split()))
ans = 10**10

for i in range(1, N):
    if i == 1:
        x = sum(a[:i])
        y = sum(a[i:])
    else:
        x += a[i-1]
        y -= a[i-1]
    check = abs(x - y)
    if ans > check:
        ans = check

print(ans)
# Original solution
N=int(eval(input()))

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

min=50000000000

for i in range(N-1):
    sunuke=sum(a[0:i+1])
    arai=sum(a[i+1:N])
    if(abs(sunuke-arai)<=min):
        min=abs(sunuke-arai)

print(min)

# Optimized solution
n = int(eval(input()))
a = [int(x) for x in input().split()]
sm = sum(a)//2
mn = 10**10
ar = a[0]
su = a[-1]
for i in range(1,len(a)-1):
    if abs(sm - ar) < mn:
        mn = abs(sm - ar)
        mn_i = i
    ar += a[i]

if len(a) > 2:
    ar = sum(a[0:mn_i])
    su = sum(a[mn_i::])
    print((abs(ar-su)))
else:
    print((abs(ar-su)))
# Original solution
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
input:
5
5
1
3
2
4

or

3
1
1
1

output:
3

or

1
"""

import sys
from bisect import bisect_left


def solve():
    dis_rec = [float('inf')] * array_length
    dis_rec[0] = array[0]
    length = 1
    for j in range(1, array_length):
        if dis_rec[length - 1] < array[j]:
            dis_rec[length] = array[j]
            length += 1
        else:
            position = bisect_left(dis_rec, array[j])
            dis_rec[position] = array[j]
    return length


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    array_length = int(_input[0])
    array = list(map(int, _input[1:]))
    print((solve()))

# Optimized solution
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
input:
5
5
1
3
2
4

or

3
1
1
1

output:
3

or

1
"""

import sys
from bisect import bisect_left


def solve():
    dis_rec = [float('inf')] * array_length
    dis_rec[0] = array[0]
    length = 1
 
    for i, ele in enumerate(array, 1):
        if dis_rec[length - 1] < ele:
            dis_rec[length] = ele
            length += 1
        else:
            position = bisect_left(dis_rec, ele)
            dis_rec[position] = ele

    return length


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    array_length = int(_input[0])
    array = list(map(int, _input[1:]))
    print((solve()))
# Original solution
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
input:
5
5
1
3
2
4

or

3
1
1
1

output:
3

or

1
"""

import sys
from bisect import bisect_left


def solve():
    dis_rec = [float('inf')] * array_length
    dis_rec[0] = array[0]
    length = 1
 
    for i, ele in enumerate(array, 1):
        if dis_rec[length - 1] < ele:
            dis_rec[length] = ele
            length += 1
        else:
            position = bisect_left(dis_rec, ele)
            dis_rec[position] = ele

    return length


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    array_length = int(_input[0])
    array = list(map(int, _input[1:]))
    print((solve()))

# Optimized solution
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
input:
5
5
1
3
2
4

or

3
1
1
1

output:
3

or

1
"""

import sys
from bisect import bisect_left


def solve():
    dis_rec = [float('inf')] * array_length
    dis_rec[0] = array[0]
    length = 1
 
    for ele in array:
        if dis_rec[length - 1] < ele:
            dis_rec[length] = ele
            length += 1
        else:
            position = bisect_left(dis_rec, ele)
            dis_rec[position] = ele

    return length


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    array_length = int(_input[0])
    array = list(map(int, _input[1:]))
    print((solve()))
# Original solution
import sys
from bisect import bisect_left

N = int(eval(input()))
inf = float("inf")
dp = [inf]*N

for n in map(int, sys.stdin.readlines()):
    dp[bisect_left(dp, n)] = n

for i, num in zip(list(range(N, 0, -1)), dp[::-1]):
    if num < inf:
        print(i)
        break

# Optimized solution
import sys
from bisect import bisect_left

N = int(eval(input()))
dp = [float("inf")]*N
result = -1

for n in map(int, sys.stdin.readlines()):
    i = bisect_left(dp, n)
    dp[i] = n
    result = i if i > result else result

print((result+1))
# Original solution
import bisect

N = int(eval(input()))
numbers = []
for i in range(N):
    numbers.append(int(eval(input())))

lis = [numbers[0]]
for last in numbers[1:]:
    pos = bisect.bisect_left(lis, last)
    if len(lis) < pos:
        lis.append(last)
    else:
        lis = lis[:pos] + [last] + lis[pos+1:]

print((len(lis)))

# Optimized solution
import bisect

N = int(eval(input()))
numbers = []
for i in range(N):
    numbers.append(int(eval(input())))

lis = [numbers[0]]
for last in numbers[1:]:
    if lis[-1] < last:
        lis.append(last)
    else:
        pos = bisect.bisect_left(lis, last)
        lis[pos] = last

print((len(lis)))
# Original solution
import bisect
n = int(eval(input()))
A = [int(eval(input())) for j in range(n)]
dp = [A[0]]
for i in range(1, n):
    if dp[-1] < A[i]:
        dp.append(A[i])
    else:
        dp[bisect.bisect_left(dp, A[i])] = A[i]
print((len(dp)))

# Optimized solution
import bisect
n = int(eval(input()))
A = [int(eval(input())) for j in range(n)]
dp = A[:1]
for a_i in A[1:]:
    if dp[-1] < a_i:
        dp.append(a_i)
    else:
        dp[bisect.bisect_left(dp, a_i)] = a_i
print((len(dp)))
# Original solution
import bisect
n = int(eval(input()))
A = [int(eval(input())) for j in range(n)]
dp = A[:1]
for a_i in A[1:]:
    if dp[-1] < a_i:
        dp.append(a_i)
    else:
        dp[bisect.bisect_left(dp, a_i)] = a_i
print((len(dp)))

# Optimized solution
import sys
import bisect
n = int(sys.stdin.readline())
A = [int(sys.stdin.readline()) for j in range(n)]
dp = A[:1]
for a_i in A[1:]:
    if dp[-1] < a_i:
        dp.append(a_i)
    else:
        dp[bisect.bisect_left(dp, a_i)] = a_i
print((len(dp)))
# Original solution
from sys import stdin
readline = stdin.readline

n = int(readline())
a = [int(readline()) for _ in range(n)]

dp = [-float('inf')]

for ai in a:
    for i in range(1, len(dp)):
        if dp[i - 1] < ai and ai < dp[i]:
            dp[i] = ai
    if dp[-1] < ai:
        dp.append(ai)

print((len(dp) - 1))

# Optimized solution
from sys import stdin
from bisect import bisect_left, bisect_right
readline = stdin.readline

dp = [-float('inf')]

for ai in (int(readline()) for _ in range(int(readline()))):
    if dp[-1] < ai:dp.append(ai)
    for i in range(bisect_right(dp, ai), bisect_left(dp, ai) + 1):dp[i] = ai
print((len(dp) - 1))
# Original solution
from sys import stdin
from bisect import bisect_left, bisect_right
readline = stdin.readline

dp = [-float('inf')]

for ai in (int(readline()) for _ in range(int(readline()))):
    if dp[-1] < ai:dp.append(ai)
    for i in range(bisect_right(dp, ai), bisect_left(dp, ai) + 1):dp[i] = ai
print((len(dp) - 1))

# Optimized solution
from sys import stdin
from bisect import bisect_left
readline = stdin.readline


dp = [-float('inf')]
n = int(readline())
a = (int(readline()) for _ in range(n))
for ai in a:
    if dp[-1] < ai:
        dp.append(ai)
    else:
        dp[bisect_left(dp, ai)] = ai
print((len(dp) - 1))
# Original solution
from bisect import bisect
n = int(eval(input()))
A = []
for i in range(n):
 A.append(int(eval(input())))
 
 L = [None for i in range(n)]
def LIS():
    L[0] = A[0]
    length = 0
    for i in range(n):
        if L[length] < A[i]:
            length += 1
            L[length] = A[i]
        else:
            if A[i] in L: continue
            j = bisect(L, A[i], 0, length)
            L[j] = A[i]
    return length + 1

print((LIS()))

# Optimized solution
from bisect import bisect
import sys
n = int(eval(input()))
A = [int(i.rstrip("\n")) for i in sys.stdin.readlines()]
 #?????????i+1??§???????¢??????¨??????????????????????´????????°???????????´?
L = []
def LIS():
    L.append(A[0])
    length = 0
    for i in range(n):
        if L[length] < A[i]:
            length += 1
            L.append(A[i])
        else:
            if A[i] in L: continue
            j = bisect(L, A[i], 0, length) #L????????§?????????A[i]??\?????¨????????????????????????
            L[j] = A[i]
    return length + 1

print((LIS()))
# Original solution
from bisect import bisect
import sys
n = int(eval(input()))
A = [int(i.rstrip("\n")) for i in sys.stdin.readlines()]
 #?????????i+1??§???????¢??????¨??????????????????????´????????°???????????´?
L = []
def LIS():
    L.append(A[0])
    length = 0
    for i in range(n):
        if L[length] < A[i]:
            length += 1
            L.append(A[i])
        else:
            if A[i] in L: continue
            j = bisect(L, A[i], 0, length) #L????????§?????????A[i]??\?????¨????????????????????????
            L[j] = A[i]
    return length + 1

print((LIS()))

# Optimized solution
from bisect import bisect_left
import sys

n = int(eval(input()))
A = [int(i.rstrip("\n")) for i in sys.stdin.readlines()]

def LIS():
    L = A[:1]
    for i in A[1:]:
        if L[-1] < i:
            L.append(i)
        else:
            j = bisect_left(L, i)
            L[j] = i
    return len(L)

print((LIS()))
# Original solution
import sys
import bisect

inf = float('inf')

def solve():
    n = int(input())
    A = [int(input()) for i in range(n)]
    dp = [inf] * n
    max_ = 1

    for a in A:
        j = bisect.bisect_left(dp, a, lo=0, hi=max_)
        max_ = max(max_, j + 1)
        dp[j] = a

    # print(dp)
    ans = max_
    print(ans)


def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None

if __name__ == '__main__':
    solve()


# Optimized solution
import sys
import bisect

inf = float('inf')

def solve():
    n = int(input())
    A = [int(input()) for i in range(n)]
    dp = [inf] * n
    # max_ = 1

    for a in A:
        j = bisect.bisect_left(dp, a)
        # max_ = max(max_, j + 1)
        dp[j] = a

    # print(dp)

    ans = 1
    for i, v in enumerate(dp):
        if v == inf:
            ans = i
            break
    else:
        ans = n
        
    print(ans)


def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None

if __name__ == '__main__':
    solve()
# Original solution
import sys
r=sys.stdin.readline
N,M=list(map(int,r().split()))
city=[[] for _ in range(N+1)]
for _ in range(M):
    a,b=list(map(int,r().split()))
    city[a].append(b)
    city[b].append(a)
check=[True]+[False]*(N)
def dfs(v):
    stack=[v]
    visited=[False]*(N+1)
    visited[v]=True
    while stack:
        cur=stack.pop()
        check[cur]=True
        for v in city[cur]:
            if not visited[v]:
                stack.append(v)
cnt=0
for i in range(1,N+1):
    if not check[i]:
        dfs(i)
        cnt+=1
print((cnt-1))

# Optimized solution
import sys
r=sys.stdin.readline
N,M=list(map(int,r().split()))
city=[[] for _ in range(N+1)]
for _ in range(M):
    a,b=list(map(int,r().split()))
    city[a].append(b)
    city[b].append(a)
check=[True]+[False]*(N)
def dfs(v):
    stack=[v]
    while stack:
        cur=stack.pop()
        check[cur]=True
        for v in city[cur]:
            if not check[v]:
                stack.append(v)
cnt=0
for i in range(1,N+1):
    if not check[i]:
        dfs(i)
        cnt+=1
print((cnt-1))
# Original solution
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし


class UnionFind:
    def __init__(self,n):
        self.par = [i for i in range(n+1)]  # 親のノード番号
        self.rank = [0]*(n+1)

    def find(self,x):  # xの根のノード番号
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def same_check(self,x,y):  # x,yが同じグループか否か
        return self.find(x) == self.find(y)

    def unite(self,x,y):  # x,yの属するグループの併合
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            x,y = y,x
        if self.rank[x] == self.rank[y]:
            self.rank[x] += 1
        self.par[y] = x



N,M = MI()
UF = UnionFind(N)
for _ in range(M):
    a,b = MI()
    if not UF.same_check(a,b):
        UF.unite(a,b)

A = UF.par
for i in range(1,N+1):
    A[i] = UF.find(A[i])

del A[0]

print((len(set(A))-1))


# Optimized solution
import sys

sys.setrecursionlimit(10**7)
def MI(): return list(map(int,sys.stdin.readline().rstrip().split()))


class UnionFind:
    def __init__(self,n):
        self.par = [i for i in range(n+1)]  # 親のノード番号
        self.rank = [0]*(n+1)

    def find(self,x):  # xの根のノード番号
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def same_check(self,x,y):  # x,yが同じグループか否か
        return self.find(x) == self.find(y)

    def unite(self,x,y):  # x,yの属するグループの併合
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            x,y = y,x
        if self.rank[x] == self.rank[y]:
            self.rank[x] += 1
        self.par[y] = x


N,M = MI()
UF = UnionFind(N)
for _ in range(M):
    a,b = MI()
    UF.unite(a,b)

A = UF.par
A = [UF.find(A[i]) for i in range(1,N+1)]

print((len(set(A))-1))
# Original solution
f=lambda:list(map(int,input().split()))
n,m=f()

# Union Find
p=[-1]*n
def root(x):
  while p[x]>=0: x=p[x]
  return x
def unite(x,y):
  x,y=root(x),root(y)
  if x==y: return
  if x>y: x,y=y,x
  p[x]+=p[y]
  p[y]=x
def same(x,y):
  return root(x)==root(y)
def size(x):
  return -p[root(x)]

for _ in range(m):
  a,b=f()
  unite(a-1,b-1)
print((sum(i<0 for i in p)-1))

# Optimized solution
f=lambda:list(map(int,input().split()))
def r(x):
  while p[x]>=0: x=p[x]
  return x
def u(x,y):
  x,y=r(x),r(y)
  if x==y: return
  if x>y: x,y=y,x
  p[x]+=p[y]
  p[y]=x
n,m=f()
p=[-1]*n
for _ in range(m):
  a,b=f()
  u(a-1,b-1)
print((sum(i<0 for i in p)-1))