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

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

relation = [[] for _ in range(N+1)]

for _ in range(M) :
	X, Y, Z = map(int, input().split())
	Z &= 1
	relation[X].append((Y, Z))
	relation[Y].append((X, Z))

visited = [False] * (N + 1)
p = [-1] * (N + 1)

def dfs(init) :
	queue = [init]
	p[init] = 0

	while queue :
		x_ = queue.pop(0)
		visited[x_] = True

		for y_, z_ in relation[x_] :
			if not visited[y_] :
				p[y_] = (z_ & 1) ^ p[x_]
				queue.append(y_)

# 閉路ごとdfs実行
n_closed = 0
for i in range(1, N + 1) :
	if len(relation[i]) and not visited[i] :
		n_closed += 1
		dfs(i)

print(n_closed + visited.count(False) - 1)


### FIXED CODE ###

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

parent = [-1] * (N + 1)

def root(x):
	while parent[x] >= 0:
		x = parent[x]
	return x

def unite(x,y):
	root_x = root(x)
	root_y = root(y)

	if root_x != root_y :
		if parent[root_x] > parent[root_y] :
			parent[root_x]=root_y
		else :
			if parent[root_x] == parent[root_y] :
				parent[root_x] -= 1
			parent[root_y] = root_x
    
	return

for _ in range(M) :
	X, Y, Z = map(int, input().split())
	unite(X, Y)
	
print(sum(i < 0 for i in parent) - 1)
### BUGGY CODE ###

n, q = map(int, input().split())
import sys
sys.setrecursionlimit(10**7)

graph = [[] for _ in range(n+1)] 
for i in range(1, n):
  a, b = map(int, input().split())
  graph[a].append(b)
  graph[b].append(a)

point = [0]*(n+1)
for i in range(q):
  p, x = map(int, input().split())
  point[p] += x

seen = [False]*(n+1)
counter = [0]*(n+1)
sum_point = 0

def dfs(v):
  
  global graph
  global seen
  global sum_point
  global counter
  
  seen[v] = True
  sum_point += point[v]
  
  for next_v in graph[v]:
    
    if seen[next_v]: 
      continue
    dfs(next_v)
  
  counter[v] = sum_point
  sum_point -= point[v]

dfs(1)
print(*counter[1::])

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7)

n, q = map(int, sys.stdin.readline().split())

graph = [[] for _ in range(n+1)] 
for i in range(1, n):
  a, b = map(int, sys.stdin.readline().split())
  graph[a].append(b)
  graph[b].append(a)

point = [0]*(n+1)
for i in range(q):
  p, x = map(int, sys.stdin.readline().split())
  point[p] += x

seen = [False]*(n+1)
counter = [0]*(n+1)
sum_point = 0

def dfs(v):
  
  global graph
  global seen
  global sum_point
  global counter
  
  seen[v] = True
  sum_point += point[v]
  
  for next_v in graph[v]:
    
    if seen[next_v]: 
      continue
    dfs(next_v)
  
  counter[v] = sum_point
  sum_point -= point[v]

dfs(1)
print(*counter[1::])
### BUGGY CODE ###

def main():
  import numpy as np
  n=int(input())
  a=list(map(int,input().split()))
  v=np.array([ai+1 for ai in a])
  v[-1]-=1
  vs=np.array([1]*n+[0])
  a[-1]-=1
  # 深さnの一本道の木を初期状態とする。
  # a[i]:深さiの葉でまだ木に連結していない葉
  nozero_vs=0
  for i in range(n+1):
    if a[i]==0:
      continue
    if nozero_vs>=i:# a[0]>0のときもここで処理
      print(-1)
      exit(0)
    # 未連結の葉を木にくっつける
    if vs[nozero_vs]>a[i]:
      vs[nozero_vs]-=a[i]
      vs[nozero_vs+1:i]+=a[i]
      v[nozero_vs+1:i]+=a[i]
    elif vs[nozero_vs]==a[i]:
      vs[nozero_vs]=0
      vs[nozero_vs+1:i]+=a[i]
      v[nozero_vs+1:i]+=a[i]
      nozero_vs+=1
    else:
      sum_vs=0
      while sum_vs<a[i]:
        if nozero_vs>=i:
          print(-1)
          exit(0)
        sum_vs+=vs[nozero_vs]
        if sum_vs<a[i]:
          b=vs[nozero_vs]
          vs[nozero_vs]=0
          vs[nozero_vs+1:i]+=b
          v[nozero_vs+1:i]+=b
          nozero_vs+=1
        else:
          b=sum_vs-a[i]
          c=vs[nozero_vs]-b
          vs[nozero_vs]=b
          vs[nozero_vs+1:i]+=c
          v[nozero_vs+1:i]+=c
  print(np.sum(v))

if __name__=='__main__':
  main()


### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
if n==0:
  if a[0]==1:
    print(1)
    exit()
  else:
    print(-1)
    exit()
if a[0]>0:
  print(-1)
  exit()

ans=1
pre_no_ha=1
all_ha=sum(a)

for ai in a[1:]:
  if pre_no_ha<=0:
    print(-1)
    exit()
  if ai>pre_no_ha*2:
    print(-1)
    exit()
  
  pre_no_ha*=2
  pre_no_ha-=ai
  all_ha-=ai
  pre_no_ha=min(pre_no_ha,all_ha)
  ans+=pre_no_ha+ai

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



def gcd(x, y): 
      
    while(y): 
        x, y = y, x % y 
      
    return x 

n=int(input())
a=list(map(int,input().split()))
x=1
ans=0
for i in a:
    x=(x*i)//gcd(x,i)
for i in a:
    ans+=(x//i)
ans=ans%(1000000007)
print(ans)

### FIXED CODE ###

def gcd(x, y): 
      
    while(y): 
        x, y = y, x % y 
      
    return x 
 
n=int(input())
a=list(map(int,input().split()))
x=1
ans=0
for i in a:
    x=(x*i)//gcd(x,i)
for i in a:
    ans+=(x//i)
ans=ans%(1000000007)
print(ans)
### BUGGY CODE ###

import numpy as np

n = int(input())
a = list(map(int, input().split()))
sum = 0
k = 0
for i in range(len(a)-1):
    for j in range(i+1,len(a)):
        k += a[j]

    
    sum += a[i] * k

    k = 0
        
ans = np.mod(sum,10**9+7)

print(ans)



### FIXED CODE ###


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

MOD = 10 ** 9 + 7

sum = sum(a) % MOD
ans = 0

for i in range(len(a)):
    sum -= a[i]
    sum %= MOD
    ans += a[i] * sum
    ans %= MOD

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

N=int(input())
a=list(map(int,input().split()))
o=[]
for i in range(N):
    o.append(a.index(min(a))+1)
    a[a.index(min(a))]=10000000
print(*o)

### FIXED CODE ###

def solve(string):
    n, *a = map(int, string.split())
    ans = [0] * n
    for i, _a in enumerate(a, 1):
        ans[_a - 1] = str(i)
    return " ".join(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))
### BUGGY CODE ###

from sys import stdin, stdout, setrecursionlimit
from collections import deque, defaultdict
from functools import lru_cache

rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()
rli = lambda: map(int, stdin.readline().split())

INF, NINF = float('inf'), float('-inf')

setrecursionlimit(10000)

def main():
	MOD = 10**9 + 7
	k, d = int(rl()), int(rl())
	ans = 0

	@lru_cache(None)
	def dfs(x, tight, i, tot):
		if i == len(x):
			return 1 if tot % d == 0 else 0
		maxdig = x[i] if tight else 9
		cnt = 0
		for dig in range(0, maxdig + 1):
			ntight = 1 if tight and dig == x[i] else 0
			cnt += dfs(x, ntight, i + 1, tot + dig)
		return cnt % MOD

	def totup(num):
		ans = deque()
		while num:
			num, dig = divmod(num, 10)
			ans.appendleft(dig)
		return tuple(ans)

	tupk = totup(k)
	ans += dfs(tupk, 1, 0, 0) - 1
	print(ans % MOD)

	stdout.close()

if __name__ == "__main__":
	main()

### FIXED CODE ###

from sys import stdin, stdout, setrecursionlimit
from collections import deque, defaultdict
from functools import lru_cache

rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()
rli = lambda: map(int, stdin.readline().split())

INF, NINF = float('inf'), float('-inf')

setrecursionlimit(100000)

def main():
	MOD = 10**9 + 7
	k, d = int(rl()), int(rl())
	ans = 0

	def totup(num):
		ans = deque()
		while num:
			num, dig = divmod(num, 10)
			ans.appendleft(dig)
		return tuple(ans)

	x = totup(k)

	dp = [[[0]*2 for j in range(d)] for i in range(len(x)+1)]
	dp[0][0][1] = 1
	for i in range(len(x)):
		for digsum in range(d):
			for tight in range(2):
				for dig in range(x[i]+1 if tight else 10):
					ns = (digsum + dig) % d 
					ntight = 1 if tight and dig == x[i] else 0
					dp[i+1][ns][ntight] += dp[i][digsum][tight]
					dp[i+1][ns][ntight] %= MOD

	ans = (dp[len(x)][0][0] + dp[len(x)][0][1] - 1) % MOD
	print(ans)
	stdout.close()

if __name__ == "__main__":
	main()
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

mod = 10**9+7
n = int(input())
g = [[] for _ in range(n)]

for _ in range(n-1):
    x, y = map(int, input().split())
    g[x-1].append(y-1)
    g[y-1].append(x-1)

white = [0 for _ in range(n)]
black = [0 for _ in range(n)]


def dfs(v):
    tmp_w = 1
    tmp_b = 1
    if len(g[v]) == 0:
        white[v] = 1
        black[v] = 1
        return None
    for w in g[v]:
        g[w].remove(v)
        dfs(w)
        tmp_w *= white[w]+black[w]
        tmp_b *= white[w]
        tmp_w %= mod
        tmp_b %= mod
    white[v] = tmp_w
    black[v] = tmp_b
    return None


dfs(0)

print((white[0]+black[0]) % mod)


### FIXED CODE ###

import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

mod = 10**9+7
n = int(input())
g = [[] for _ in range(n)]

for _ in range(n-1):
    x, y = map(int, input().split())
    g[x-1].append(y-1)
    g[y-1].append(x-1)

white = [0 for _ in range(n)]
black = [0 for _ in range(n)]


def dfs(v):
    tmp_w = 1
    tmp_b = 1
    if len(g[v]) == 0:
        white[v] = 1
        black[v] = 1
        return None
    for w in g[v]:
        g[w].remove(v)
        dfs(w)
        tmp_w *= white[w]+black[w]
        tmp_b *= white[w]
        tmp_w %= mod
        tmp_b %= mod
    white[v] = tmp_w
    black[v] = tmp_b
    return None


dfs(0)

print((white[0]+black[0]) % mod)
### BUGGY CODE ###

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,m = map(int, input().split())
from collections import defaultdict
ns = defaultdict(set)

for i in range(m):
    a,b = map(int, input().split())
    ns[a-1].add(b-1)

seen = [False] * n
done = [False] * n
hist = []
def dfs(u, pos):
    # uからdfs
    seen[u] = True
    hist.append(u)
    for v in ns[u]:
        if done[v]:
            continue
        elif seen[v]:
            # サイクルを検出
            pos = v
            return pos
        else:
            pos = dfs(v, pos)
            if pos>=0:
                return pos
    done[u] = True
    hist.pop()
    return pos
    
for u in range(n):
    pos = dfs(u, -1)
    if pos>=0:
        while True:
            us = set(hist)
            update = False
            for i,u in enumerate(hist):
                nu = hist[(i+1)%len(hist)]
                if ns[u]&us-set([nu]):
                    for v in ns[u]&us-set([nu]):
                        break
                    j = hist.index(v)
                    if i<j:
                        hist = hist[i:] + hist[:j+1]
                    else:
                        hist = hist[j:i+1]
                    update = True
                    break
            if not update:
                break
        print(len(hist))
        write("\n".join(map(lambda x: str(x+1), hist)))
        break
else:
    print(-1)

### FIXED CODE ###

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,m = map(int, input().split())
from collections import defaultdict
ns = defaultdict(set)

for i in range(m):
    a,b = map(int, input().split())
    ns[a-1].add(b-1)

seen = [False] * n
done = [False] * n
hist = []
def dfs(u, pos):
    # uからdfs
    seen[u] = True
    hist.append(u)
    for v in ns[u]:
        if done[v]:
            continue
        elif seen[v]:
            # サイクルを検出
            pos = v
            return pos
        else:
            pos = dfs(v, pos)
            if pos>=0:
                return pos
    done[u] = True
    hist.pop()
    return pos
def cycle(hist, pos):
    for i, u in enumerate(hist):
        if u==pos:
            break
    return hist[i:]

for u in range(n):
    pos = dfs(u, -1)
    if pos>=0:
        hist = cycle(hist, pos)
        while True:
            us = set(hist)
            update = False
            for i,u in enumerate(hist):
                nu = hist[(i+1)%len(hist)]
                pu = hist[(i-1)]
                if (ns[u]&us)-set([nu, pu]):
                    for v in (ns[u]&us)-set([nu, pu]):
                        break
                    j = hist.index(v)
                    if i<j:
                        hist = hist[i:] + hist[:j+1]
                    else:
                        hist = hist[j:i+1]
                    update = True
                    break
            if not update:
                break
        print(len(hist))
        write("\n".join(map(lambda x: str(x+1), hist)))
        break
else:
    print(-1)
### BUGGY CODE ###

n = int(input())
a = []
for i in range(n):
    a.append(int(input()))

max_val = max(a)

for i in range(n):
    if max_val > a[i]:
        print(max_val)

    elif max_val == a[i]:
        temp = a[i]
        a[i] = 0
        print(max(a))
        a[i] = temp

### FIXED CODE ###

n = int(input())
a = []
for i in range(n):
    a.append(int(input()))

max_val = max(a)
max_total = a.count(max_val) - 1

for i in range(n):
    if max_val > a[i]:
        print(max_val)

    elif max_val == a[i] and max_total != 0:
        print(max_val)
        max_total -= 1
    else:
        temp = a[i]
        a[i] = 0
        print(max(a))
        a[i] = temp
### BUGGY CODE ###

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

ENDED = 0
FIRST = 1
UP = 2
DOWN = 3
EVEN = 4

flg = ENDED
ret = 0
bfr = None

i = 0
while i < n:
    a_ = a[i]
    if flg == ENDED:
        ret += 1
        flg = FIRST
        i += 1
    elif flg == FIRST:
        if a_ > bfr:
            flg = UP
        elif a_ == bfr:
            flg = EVEN
        elif a_ < bfr:
            flg = DOWN
        i += 1
    elif flg == UP:
        if a_ < bfr:
            flg = ENDED
        else:
            i += 1
    elif flg == EVEN:
        if a_ > bfr:
            flg = UP
        elif a_ < bfr:
            flg = DOWN
    elif flg == DOWN:
        if a_ > bfr:
            flg = ENDED
        else:
            i += 1
    
    bfr = a_
        
print(ret)

### FIXED CODE ###


n = int(input())
a_ = list(map(int, input().split(' ')))
a = [-1]
i = 0
while  i < n:
    if a[-1] != a_[i]:
        a.append(a_[i])
    i += 1
a = a[1:]
n = len(a)

ret = 1

i = 0
while i < n-2:
    if (a[i] > a[i+1] and a[i+1] < a[i+2]) or (a[i] < a[i+1] and a[i+1] > a[i+2]):
        ret += 1
        i += 2
    else:
        i += 1
        
print(ret)
### BUGGY CODE ###

n=int(input())
a=list(map(int,input().split()))
for i in range(1,n+1):
  print(a.count(i))

### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
x=[0]*n
for i in range(n-1):
  x[a[i]-1]+=1
for j in range(n):
  print(x[j])
### BUGGY CODE ###

from collections import defaultdict
from heapq import *
import sys

int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

n,m=MI()
to=defaultdict(list)
inf=10**9
dist=defaultdict(int)
home=[set() for _ in range(n)]
for u in range(n):dist[u,-1]=inf
for _ in range(m):
    u,v,c=MI1()
    to[u,c].append(v)
    to[v,c].append(u)
    home[u].add(c)
    home[v].add(c)
    dist[u,c]=dist[v,c]=inf

hp=[]
dist[0,-1]=0
heappush(hp,(0,0,-1))
while hp:
    d,u,c=heappop(hp)
    #print(d,u,c,hp,dist)
    if u==n-1:
        print(d)
        break
    if d>dist[u,c]:continue
    if c==-1:
        for nc in home[u]:
            if d+1<dist[u,nc]:
                dist[u,nc]=d+1
                heappush(hp,(d+1,u,nc))
    else:
        if d<dist[u,-1]:
            dist[u,-1]=d
            heappush(hp,(d,u,-1))
        for v in to[u,c]:
            if d>=dist[v,c]:continue
            dist[v,c]=d
            heappush(hp,(d,v,c))
else:
    print(-1)


### FIXED CODE ###

import sys

int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

from collections import deque

def solve():
    dist = [inf] * len(to)
    q = deque()
    q.append((0, 0))
    dist[0] = 0
    while q:
        d, i = q.popleft()
        if d > dist[i]: continue
        if i < n:
            for j in to[i]:
                if dist[j] <= d + 1: continue
                dist[j] = d + 1
                q.append((d + 1, j))
        else:
            for j in to[i]:
                if j == n - 1:
                    print(dist[i])
                    exit()
                if dist[j] <= d: continue
                dist[j] = d
                q.appendleft((d, j))
    print(-1)

inf=10**9
n,m=MI()
to=[]
uctoi={}

for u in range(n):
    to.append([])
    uctoi[u,0]=u

for _ in range(m):
    u,v,c=MI()
    u,v=u-1,v-1
    if (u,c) not in uctoi:
        i=uctoi[u,c]=len(to)
        to.append([])
        to[i].append(u)
        to[u].append(i)
    else:i=uctoi[u,c]
    if (v,c) not in uctoi:
        j=uctoi[v,c]=len(to)
        to.append([])
        to[j].append(v)
        to[v].append(j)
    else:j=uctoi[v,c]
    to[i].append(j)
    to[j].append(i)
#print(to)
#print(fin)

solve()
### BUGGY CODE ###

N, M = map(int, input().split())
AB = {}
for i in range(N):
    a, b = map(int, input().split())
    if a in AB.keys():
        AB[a].append(b)
    else:
        AB[a] = [b]
dp = [0] * (M+1)
for i in range(1, M+1):
    tmp = 0
    km = -1
    tm = -1
    for k in range(i+1):
        if k not in AB.keys():
            continue
        for t in range(len(AB[k])):
            if tmp < AB[k][t]:
                tmp = AB[k][t]
                km = k
                tm = t
    if km >= 0:
        dp[i] = dp[i-1] + tmp
        AB[km][tm] = 0
    else:
        dp[i] = dp[i-1]

print(dp[len(dp)-1])


### FIXED CODE ###

from heapq import heappush, heappop
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort(reverse=True)
hp = []
ret = 0
for i in range(1, M+1):
    # push
    while len(AB) > 0 and AB[len(AB)-1][0] <= i:
        heappush(hp, AB.pop()[1]*-1)
    # pop
    if len(hp) > 0:
        ret += heappop(hp)*-1

print(ret)
### BUGGY CODE ###

import math
import string
import collections
from collections import Counter
from collections import deque


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


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


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
n = int(input())
a = readints()
ans = 1
for i in range(n):
    ans *= a[i]
if ans > 10**18:
    print(-1)
else:
    print(ans)


### FIXED CODE ###

import math
import string
import collections
from collections import Counter
from collections import deque


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


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


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
n = int(input())
a = readints()
ans = 1
if 0 in a:
    print(0)
    exit()
for i in range(n):
    ans *= a[i]
    if ans > 10**18:
        print(-1)
        exit()
print(ans)
### BUGGY CODE ###

import sys
input = sys.stdin.readline

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

from collections import Counter

C=Counter(A)

LIST=sorted(C.values())
LEN=len(LIST)
SUM=sum(LIST)

for k in range(1,N+1):
    if k>LEN:
        print(0)
    else:
        BASE=LIST[-k:]
        #print(BASE)

        REST=SUM-sum(BASE)
        REST0=REST

        MIN=min(BASE)
        MAX=max(BASE)

        if MAX*k-sum(BASE)<=REST:
            print(SUM//k)
            continue

        while MIN!=MAX:
            mid=(MIN+MAX+1)//2
            REST=REST0

            for b in BASE:
                if b<mid:
                    REST-=mid-b
                if REST<0 or b>=mid:
                    break

            #print(MAX,MIN,mid,REST)

            if REST>=0:
                MIN=mid
            else:
                MAX=mid-1

        print(MIN)


### FIXED CODE ###

import sys
input = sys.stdin.readline

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

from collections import Counter
from itertools import accumulate
import bisect

C=Counter(A)

LIST=sorted(C.values())
LEN=len(LIST)
SUM=[0]+list(accumulate(LIST))
MIN=0
MAX=SUM[-1]

for k in range(1,N+1):
    MIN=0
    while MIN!=MAX:
        mid=(MIN+MAX+1)//2
        x=bisect.bisect_right(LIST,mid)

        if SUM[x]+(LEN-x)*mid>=mid*k:
            MIN=mid
        else:
            MAX=mid-1

    print(MIN)
### BUGGY CODE ###

k, s = map(int, input().split())
cnt = 0
for i in range(k+1):
    for j in range(k+1):
        for k in range(k+1):
            if i + j + k == s:
                cnt += 1
print(cnt)

### FIXED CODE ###

k, s = map(int, input().split())
cnt = 0
for x in range(k+1):
    for y in range(k+1):
        if 0 <= s-x-y <= k:
            cnt += 1
print(cnt)
### BUGGY CODE ###

import math
from functools import reduce
a=1;b=1;c=1;d=0
n=int(input());
for i1 in range(n):
	for i2 in range(n):
		for i3 in range(n):
			d+=reduce(math.gcd,(a,b,c))
			c+=1
		b+=1;c=1;i3=0
	a+=1;b=1;i2=0
print(d)

### FIXED CODE ###

import math
from functools import reduce
a=1;b=1;c=1;d=0
n=int(input());
for i1 in range(n):
	for i2 in range(n):
		for i3 in range(n):
			d+=reduce(math.gcd,(a,b,c))
			c+=1
		b+=1;c=1;i3=0 #毎回b,cをリセットさせないと増え続ける
	a+=1;b=1;i2=0 #i2,i3もリセットしないとどうなるのかはわからんけど変になる
print(d)
### BUGGY CODE ###

import array
import heapq
def SME(start,end):
    A = []
    if end > N : end = N
    count = 0
    for i,x in enumerate(Whole[start:end]):
        heapq.heappush(A,[x,i])
    for j in range(start,end):
        a = heapq.heappop(A)
        for i in range(L):
            idx = a[1] + i
            try :
                if ANS[idx]==0 : continue
                else :
                    ANS[idx] = a[0]
                    count += 1
            except IndexError : continue
        if count > N * parameter : break
    for i in range(L):
        if ANS[i]==0 : ANS[i] = min(Whole[:i+1])
    for i in range(L,N):
        if ANS[i]==0 : ANS[i] = min(Whole[i+1-L:i+1])
    return ANS
 
[N,L] = [int(x) for x in input().split()]
Whole = [int(x) for x in input().split()]
parameter = 0.9907
ANS = array.array('I',[0]*N)
ANS = SME(0, N)
for i in ANS[L-1:-1]:print(i, end=" ")
print(ANS[-1])

### FIXED CODE ###

def ascend(a,b):
    mini = min(A[a:a+L])
    ans[a] = mini
    for i in range(a+1,b):
        if A[i+L-1] <= mini : mini = A[i+L-1]
        elif A[i-1] == mini : mini = min(A[i:i+L])
        ans[i] = mini

def descend(a,b):
    mini = min(A[b-1:b+L-1])
    ans[b-1] = mini
    for i in range(b-2,a-1,-1):
        if A[i] <= mini : mini = A[i]
        elif A[i+L] == mini: mini = min(A[i:i+L])
        ans[i] = mini
    
[N,L] = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
ans = [None]*(N-L+1)
count = 0;
if N > 10 ** 4:
    for i in range(100):
        if A[i+1] > A[i] : count += 1
    if count > 80 : descend(0, N-L+1)
    else : ascend(0,N-L+1)
else :
    ascend(0, N-L+1)
for i in ans[:-1] : print(i, end=" ")
print(ans[-1])
### BUGGY CODE ###

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

costs = [0 for x in range(N-1)]
for i in range(1, N):
  costs[i-1] = abs(sum(A[:i]) - sum(A[i:]))
  
print(min(costs))

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
total = sum(A)
costs = [0 for x in range(N-1)]
mem = [0 for x in range(N-1)]

for i in range(1, N):
  if i == 1:
    mem[i-1] = A[i-1] * 2
  else:
    mem[i-1] = mem[i-2] + A[i-1] * 2
  costs[i-1] = abs(total - mem[i-1])
  
print(min(costs))