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

from bisect import bisect

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

A.sort()
B.sort()
C.sort()

ans = 0

for a in A:
    i = bisect(B, a)
    for b in B[i:]:
        j = bisect(C, b)
        ans += N - j

print(ans)

### FIXED CODE ###

from bisect import bisect
import random


def binary_search(L, n, i, j):
    # print(L, n, i, j)
    low = i
    high = j

    while low <= high:
        mid = (low + high) //2
        guess = L[mid]
        # if guess == n:
            # return mid
        if guess > n:
            high = mid -1
        else:
            low = mid + 1
    # print(low)
    return low

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
# N = 10 ** 5
# A = [random.randint(1, 10**9) for _ in range(N)]
# B = [random.randint(1, 10**9) for _ in range(N)]
# C = [random.randint(1, 10**9) for _ in range(N)]

A.sort()
B.sort()
C.sort()

ans = 0
i, j = 0, 0

A_ind = []
B_cum = [0]

for a in A:
    # i = binary_search(B, a, i, N-1)
    i = bisect(B, a)
    A_ind.append(i)

s = 0
for b in B:
    # j = binary_search(C, b, j, N-1)
    j = i = bisect(C, b)
    s += N-j
    B_cum.append(s)

for a in A_ind:
    ans += B_cum[-1] - B_cum[a]
print(ans)
### BUGGY CODE ###

MOD = 998244353

N = int(input())
a = [int(input()) for _ in range(N)]

total = sum(a)
sup = (total - 1) // 2

# total > 2 * max(R,G,B)
# を満たすR,G,Bの場合の数
# (total - 1) // 2 >= max(R,G,B)

dp = [[[0 for _ in range(total + 1)] for _ in range(total + 1)] for _ in range(N + 1)]
dp[0][0][0] = 1
# dp[i][R][G]:=i番目まででRGとなる総数

# 枝刈り
# sup = (total - 1) // 2
# sup >= max(R,G,B)
# supを超えるRGBはdp遷移時にカット

for i in range(N):
    # a[i]はRGBのいずれかに加算
    for s in range(2 * sup + 1):
        for r in range(a[i], min(s, sup) + 1):
            g = s - r
            dp[i + 1][r][g] += dp[i][r - a[i]][g]
            dp[i + 1][r][g] %= MOD
        for g in range(a[i], min(s, sup) + 1):
            r = s - g
            dp[i + 1][r][g] += dp[i][r][g - a[i]]
            dp[i + 1][r][g] %= MOD
        for r in range(min(s, sup) + 1):
            g = s - r
            dp[i + 1][r][g] += dp[i][r][g]
            dp[i + 1][r][g] %= MOD
            # Bに対応

ans = sum(dp[N][r][g] for r in range(sup + 1) for g in range(sup + 1) if total - r - g <= sup)
ans %= MOD
print(ans)


### FIXED CODE ###

# https://atcoder.jp/contests/tenka1-2019/submissions/5065329

import sys

input = sys.stdin.readline

N = int(input())
a = [int(input()) for _ in range(N)]
total = sum(a)
MOD = 998244353

'''(1)全体'''
U = pow(3, N, MOD)  # 三角形を構成できない場合を含むRGBの組み合わせ総数

'''(2)単円'''
dp = [[0] * (total + 1) for _ in range(N + 1)]
dp[0][0] = 1
# dp[i][R]:=i番目まででRとなる場合の数

for i in range(N):
    for r in range(total + 1):
        if r - a[i] >= 0:
            dp[i + 1][r] += dp[i][r - a[i]]
        dp[i + 1][r] += 2 * dp[i][r]
        dp[i + 1][r] %= MOD
        # g, bに加算する分で、2回起こる
# print(dp)

single_circle = 0
for r in range(total + 1):
    if r >= (total / 2):
        single_circle += dp[N][r]
# print(single_circle)

'''(3)２円の積集合'''
if total % 2 == 0:
    dp2 = [[0] * (total + 1) for _ in range(N + 1)]
    dp2[0][0] = 1
    # dp[i][R]:=i番目まででRとなる場合の数

    for i in range(N):
        for r in range(int(total / 2) + 1):
            if r - a[i] >= 0:
                dp2[i + 1][r] += dp2[i][r - a[i]]
            dp2[i + 1][r] += dp2[i][r]
            dp2[i + 1][r] %= MOD
    # print(dp2)

two_circle_intersect = dp2[N][int(total / 2)] if total % 2 == 0 else 0

'''(3)３円の積集合'''
three_circle_intersect = 0

'''(4)解答'''
ans = 0

ans += U
ans %= MOD

ans -= 3 * single_circle
ans %= MOD

ans += 3 * two_circle_intersect
ans %= MOD

ans -= three_circle_intersect
ans %= MOD

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

from collections import defaultdict
import sys
input = sys.stdin.readline
exit = sys.exit
N = int(input().strip())
S = input().strip()
half_N = N//2
cand = []
flag = True
for i in range(N):
    tmp = []
    for j in range(1, N-i-1):
        if S[i] == S[i+j]:
            flag = False
            tmp.append(i+j)
    cand.append(tmp)
if flag:
    print(0)
    exit()

for length in range(1, N//2+1):
    flag = True
    new_cand = []
    for i in range(N-length):
        tmp = []
        now_check = S[i+length]
        for c in cand[i]:
            if i + length >= c or c + length >= N:
                continue
            if S[c+length] == now_check:
                flag = False
                tmp.append(c)
        new_cand.append(tmp)
    cand = new_cand
    if flag:
        print(length)
        exit()

print(length+1)


### FIXED CODE ###

import sys
input = sys.stdin.readline


def main():
    N = int(input())
    S = input().strip()

    dp = [0] * N
    for i, s in enumerate(S):
        if i == 0:
            continue
        before_ans = dp[i-1]
        now_S = S[:i+1]
        tmp_ans = now_S[-(before_ans+1):i+1]
        # print(i, before_ans, tmp_ans)
        if tmp_ans in now_S[:-(before_ans+1)]:
            dp[i] = before_ans + 1
        else:
            dp[i] = before_ans
    print(dp[-1])


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

#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

def solve():
    n = I()
    edge = [[] for _ in range(n)]
    for v in range(1,n):
        u = I()-1
        edge[u].append(v)

    dp = [None]*n
    def dfs(s):
        if dp[s] != None:
            return dp[s]
        if edge[s] == []:
            dp[s] = 0
            return 0
        tmp = []
        for v in edge[s]:
            tmp.append(dfs(v))
        dp[s] = max(1+i+x for i,x in enumerate(sorted(tmp,reverse=1)))
        return dp[s]
    print(dfs(0))
    
if __name__=='__main__':
    solve()



### FIXED CODE ###

#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))


n = I()
edge = [[] for _ in range(n)]
for v in range(1,n):
    u = I()-1
    edge[u].append(v)

dp = [None]*n
def dfs(s):
    if dp[s] != None:
        return dp[s]
    if edge[s] == []:
        dp[s] = 0
        return 0
    tmp = []
    for v in edge[s]:
        tmp.append(dfs(v))
    tmp.sort(reverse=True)
    m = 0
    for i,j in enumerate(tmp):
        m = max(m,(i+1)+j)
    dp[s] = m
    return m

dfs(0)
print(dp[0])
### BUGGY CODE ###

import sys
import numpy as np
H,W,M = list(map(int, input().split()))

hw = [[0] * W for _ in range(H)]
hw = np.array(hw)

for i in range(M):
    h, w = list(map(int, input().split()))
    hw[h-1,w-1] = 1

a = 0
for i in range(H):
    h = np.sum(hw[i,:])
    for j in range(W):
        w = np.sum(hw[:,j])
        d = hw[i,j]
        a = max(a, h+w-d)
print(a)

### FIXED CODE ###

H,W,M = list(map(int, input().split()))

hw = set()
w = [0] * 3*10**5
h = [0] * 3*10**5

for i in range(M):
    h_i, w_j = map(int, input().split())
    hw.add((h_i-1) + (w_j-1)*3*10**6)
    h[h_i-1] += 1
    w[w_j-1] += 1

a = 0
d = 1
mxh_v = max(h)
mxw_v = max(w)

x = [i for i,j in enumerate(h) if j == mxh_v]
y = [i for i,j in enumerate(w) if j == mxw_v]

for i in x:
    for j in y:
        if not(i + j*3*10**6 in hw):
            print(mxh_v+mxw_v)
            exit()
print(mxh_v+mxw_v-d)
### BUGGY CODE ###

k, s = map(int, input().split())
ans = 0
for x in range(k+1):
    for y in range(k+1):
        for z in range(s-x-y+1):
           if x+y+z==s and 0<=z<=k:
               ans += 1
print(ans)


### FIXED CODE ###

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

# -*- coding: utf-8 -*-
from collections import defaultdict

def solve():
    N, M = map(int, input().split())
    F = defaultdict(list)
    D = defaultdict(int)
    for _ in range(M):
        a, b = map(int, input().split())
        D[a] += 1
        D[b] += 1
        F[a] += F[a] + [b]
        F[b] += F[b] + [a]

    E = defaultdict(int)
    for d in D.values():
        E[d] += 1



    if all(k%2==0 for k in E.keys()) and (sum(E[k] for k in E.keys() if k >=6)>=1 or E[4]>=3):
        res = 'Yes'
    elif E[4]==2:
        x, y = [a for a,d in D.items() if d == 4]
        q = set((y,))
        R = set()
        while q:
            z = q.pop()
            if z != x:
                R.add(z)
                q |= set(F[z])-R
        if set(F[x])&R == set(F[x]):
            res = 'No'
        else:
            res = 'Yes'
    else:
        res = 'No'
    return str(res)
if __name__ == '__main__':
    print(solve())


### FIXED CODE ###

# -*- coding: utf-8 -*-

def solve():
    N, M = map(int, input().split())
    F = [list() for _ in range(N+1)]
    D = [int() for _ in range(N+1)]
    for _ in range(M):
        a, b = map(int, input().split())
        D[a] += 1
        D[b] += 1
        F[a].append(b)
        F[b].append(a)

    E = [0 for _ in range(7)]
    X = list()
    for a,d in enumerate(D[1:], start=1):
        if d%2==0:
            if d >= 6:
                E[6] += 1
            elif d == 4:
                E[4] += 1
                X.append(a)
            else:
                E[d] += 1
        else:
            return 'No'
            E[1] += 1

    if E[6]>0 or E[4]>2:
        return 'Yes'

    elif E[4]<2:
        return 'No'

    else:
        x, y = X
        q = set((y,))
        R = set((x,))
        while q:
            z = q.pop()
            R.add(z)
            q |= set(F[z])-R
        if set(F[x])&R == set(F[x]):
            return 'No'
        else:
            return 'Yes'

if __name__ == '__main__':
    print(solve())
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**7)

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

# 木構造をリストで表現する
G = [[] for _ in range(N+1)]
for i in range(N-1):
    a, b = map(int, input().split())
    G[a].append(b)
    G[b].append(a)

# 各操作で与えられる値を、各頂点ごとに集計しておく
PX = [[int(x) for x in input().split()] for _ in range(Q)]

P = [0] * (N+1)
for p, x in PX:
    P[p] += x


def dfs(v, parent, point):
    # 現在いる頂点vの親までの値をpointとする
    P[v] += point

    for next_v in G[v]:
        if next_v == parent:
            continue
        dfs(next_v, v, P[v])


dfs(1, 0, 0)

res = ' '.join(map(str, P[1:]))
print(res)

### FIXED CODE ###

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

 
N,Q = map(int,input().split())
AB = [[int(x) for x in input().split()] for _ in range(N-1)]
PX = [[int(x) for x in input().split()] for _ in range(Q)]
 
graph = [[] for _ in range(N+1)]
for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)
 
value = [0] * (N+1)
for p,x in PX:
    value[p] += x
 
def dfs(v,parent,add):
    value[v] += add
    for x in graph[v]:
        if x == parent:
            continue
        dfs(x,v,value[v])
 
dfs(1,0,0)
 
answer = ' '.join(map(str,value[1:]))
print(answer)
### BUGGY CODE ###

n = int(input())
p = [int(e) for e in input().split()]
count=0
p_min=p[0]

for i in range(n):
    if p[i] <= p_min:
        count+=1
        p_min=min(p[0:i+1])

print (count)

### FIXED CODE ###

n = int(input())
p = [int(e) for e in input().split()]
count=0
p_min=p[0]

for i in range(n):
    if p[i] <= p_min:
        count+=1
#        p_min=min(p[0:i+1])
        p_min=p[i]

print (count)
### BUGGY CODE ###

N = int(input())
S = input()

tmp = [int(p.replace('W', '-1')) if p == 'W' else int(p.replace('E', '1')) for p in list(S)]

result = []
cnt = 0
for CTO in range(N):
    for MEM in range(N):
        if MEM == CTO:
            continue
        if CTO < MEM:
            if tmp[CTO] == -1:
                if (tmp[MEM] + tmp[CTO]) == 0:
                    cnt += 1
            if tmp[CTO] == 1:
                if (tmp[MEM] + tmp[CTO]) != 0:
                    cnt += 1

        if MEM < CTO:
            if tmp[CTO] == -1:
                if (tmp[MEM] + tmp[CTO]) != 0:
                    cnt += 1
            if tmp[CTO] == 1:
                if  (tmp[MEM] + tmp[CTO]) == 0:
                    cnt += 1
    result.append(cnt)
    cnt = 0

result_x = [ result[i] for i in range(N) if result[i] != 0]
print(min(result_x))

### FIXED CODE ###

N = int(input())
S = list(input())

w = 0
e = S[1:].count('E')

leader = S[0]
min_mem=e
cnt_mem=0

for i in range(1, N):
    if leader ==  'W':
        w += 1
    leader = S[i]

    if leader == 'E':
        e -= 1

    cnt_mem = w + e
    if cnt_mem < min_mem:
        min_mem = cnt_mem

print(min_mem)
### BUGGY CODE ###

n=int(input())
a = list(map(int, input().strip().split()))
total=0
for i in range(len(a)):
    max_num=max(a[:i+1])
    total=total+max_num-a[i]
print(total)

### FIXED CODE ###

def maxof(x, y):
    if x > y:
        return x
    return y

n=int(input())
a = list(map(int, input().strip().split()))
total=0
max_num=0
for i in range(len(a)):
    max_num=maxof(max_num,a[i])
    total=total+max_num-a[i]
print(total)
### BUGGY CODE ###

n=int(input())
a=list(map(int,input().split()))
s=[0]*60
m=10**9+7
for i in a:
    t=bin(i)[2:][::-1]
    for i in range(len(t)):
        if t[i]=="1":
            s[i]+=1
c=0
for i in range(60):
    c=(c+s[i]*(n-s[i])*pow(2,i,m))%m
print(c)

### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
s=[0]*60
m=10**9+7
for i in a:
    t=bin(i)[2:][::-1]
    for i in range(len(t)):
        if t[i]=="1":
            s[i]+=1
c=0
for i in range(60):
    c=(c+s[i]*(n-s[i])*pow(2,i,m))%m
print(c)
### BUGGY CODE ###

N = int(input())

ARR = [int(s) for s in input().split(" ")]

import math

def calculate(n,arr):
    srr = [arr - index - 1 for index,arr in enumerate(arr)]

    maxValue = max(srr)
    minValue = min(srr)

    if maxValue * minValue < 0:
        if math.fabs(maxValue) > math.fabs(minValue):
            pinyizuidaxiang =  int(math.fabs(maxValue))
        else:
            pinyizuidaxiang = int(math.fabs(minValue))
    elif maxValue * minValue > 0:
        pinyizuidaxiang = int(math.fabs(maxValue))
    else:
        if math.fabs(maxValue) != 0:
            pinyizuidaxiang = int(math.fabs(maxValue))
        if math.fabs(minValue) != 0:
            pinyizuidaxiang = int(math.fabs(minValue))
        if (math.fabs(maxValue) == 0)  and (math.fabs(minValue) == 0):
            pinyizuidaxiang = 0


    minest = []

    if pinyizuidaxiang == 0:
        return sum(srr)

    for i in range(-pinyizuidaxiang,pinyizuidaxiang):
        minest.append(sum([math.fabs(sr -i) for sr in srr]))

    return int(min(minest))

result = calculate(N,ARR)
print(result)



### FIXED CODE ###


N = int(input())

ARR = [int(s) for s in input().split(" ")]

import numpy as np
def calculate(n,arr):
    srr = np.array([arr - index - 1 for index,arr in enumerate(arr)])

    srr = sorted(srr)

    if len(srr) % 2 == 0:
        centerIndex = len(srr)//2
        b = (srr[centerIndex] + srr[centerIndex-1])//2
    else:
        centerIndex = (len(srr) -1) //2
        b = srr[centerIndex]



    return sum(np.fabs(srr - b))

result = int(calculate(N,ARR))

print(result)
### BUGGY CODE ###

from heapq import heapify, heappush, heappop

import sys
sys.setrecursionlimit(100000000)

n = int(input())
G = [[] for _ in range(n + 1)]

for _ in range(n - 1):
    ai, bi, ci = map(int, input().split())
    G[ai].append((bi, ci))
    G[bi].append((ai, ci))

Q, K = map(int, input().split())

# 頂点KからDFSする
d = [None for _ in range(n + 1)]
d[K] = 0

def dfs(v, dist):
    for (u, cvu) in G[v]:
        if d[u] is None:
            d[u] = dist + cvu
            dfs(u, dist + cvu)

dfs(K, 0)

for _ in range(Q):
    xi, yi = map(int, input().split())
    print(d[xi] + d[yi])


### FIXED CODE ###

from heapq import heapify, heappush, heappop

import sys
sys.setrecursionlimit(100000000)

n = int(input())
G = [[] for _ in range(n + 1)]

for _ in range(n - 1):
    ai, bi, ci = map(int, input().split())
    G[ai].append((bi, ci))
    G[bi].append((ai, ci))

Q, K = map(int, input().split())

# # 頂点KからDFSする
# d = [None for _ in range(n + 1)]
# d[K] = 0

# def dfs(v, dist):
#     for (u, cvu) in G[v]:
#         if d[u] is None:
#             d[u] = dist + cvu
#             dfs(u, dist + cvu)

# dfs(K, 0)

# 頂点KからDFSする (heap ver.)
queue = []
d = [None for _ in range(n + 1)]
heappush(queue, (0, K, -1))
while len(queue) > 0:
    dist, index, parent = heappop(queue)
    if d[index] is not None:
        continue
    d[index] = dist
    for (u, cvu) in G[index]:
        if u == parent:
            continue
        heappush(queue, (dist + cvu, u, index))

for _ in range(Q):
    xi, yi = map(int, input().split())
    print(d[xi] + d[yi])
### BUGGY CODE ###

import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

K = int(input())
ans = 0
for a in range(1,K+1):
    for b in range(1,K+1):
        for c in range(1,K+1):
            ans += gcd(a,b,c)
print(ans)

### FIXED CODE ###

from math import gcd
K = int(input())
ans = 0
for a in range(1,K+1):
    for b in range(1,K+1):
        for c in range(1,K+1):
            ans += gcd(gcd(b,c),a)
print(ans)
### BUGGY CODE ###

N = int(input())
As = list(map(int, input().split()))
modNum = 10**9 + 7
sum = 0
for d in range(60):
	n0 = 0
	n1 = 0
	for A in As:
		if (A >> d) & 1 == 1:
			n1 += 1
		else:
			n0 += 1
	sum += (1 << d) * n1 * n0
print(sum % modNum)

### FIXED CODE ###

import numpy as np
N = int(input())
As = np.array(list(map(int, input().split())))
modNum = 10**9 + 7
sum = 0
for d in range(60):
	n1 = np.count_nonzero((As >> d) & 1 == 1)
	sum += (2 ** d) * n1 * (N - n1)
print(sum % modNum)
### BUGGY CODE ###

import os, sys, re, math

(N, Q) = [int(n) for n in input().split()]
S = input()

oc = []
for i in range(len(S) - 1):
    if S[i:i+2] == 'AC':
        oc.append(i)

L = []
R = []
for i in range(Q):
    (l, r) = [int(n) for n in input().split()]
    L.append(l - 1)
    R.append(r - 1)

for i in range(Q):
    print(len(list(filter(lambda x:x >= L[i] and x < R[i], oc))))


### FIXED CODE ###

import os, sys, re, math

(N, Q) = [int(n) for n in input().split()]
S = input()

oc = [0]
n = 0
for i in range(len(S) - 1):
    if S[i:i+2] == 'AC':
        n += 1
    oc.append(n)
oc.append(n)

for i in range(Q):
    (l, r) = [int(n) for n in input().split()]
    print(oc[r - 1] - oc[l - 1])
### BUGGY CODE ###

#!/usr/bin python3
# -*- coding: utf-8 -*-

N, K = map(int, input().split())
A = list(map(int, input().split()))
Hk = 1
for i in range(K):
    Hk*=A[i]

for i in range(K,N):
    pv=Hk
    Hk=Hk*A[i]//A[i-K]
#    print(Hk)
    if Hk>pv:
        print('Yes')
    else:
        print('No')


### FIXED CODE ###

#!/usr/bin python3
# -*- coding: utf-8 -*-

N, K = map(int, input().split())
A = list(map(int, input().split()))
for i in range(K,N):
    if A[i]>A[i-K]:
        print('Yes')
    else:
        print('No')
### BUGGY CODE ###

import sys

input = sys.stdin.readline


def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    for i in range(M):
        B, C = map(int, input().split())
        A.extend([C] * B)
    A.sort()
    ans = sum(A[-N:])
    print(ans)


if __name__ == "__main__":
    main()


### FIXED CODE ###

N, M = [int(i) for i in input().strip().split()]
A = [int(i) for i in input().strip().split()]
A.sort()

BC = [[0, 0] for _ in range(M)]
for j in range(M):
    BC[j][0], BC[j][1] = [int(i) for i in input().strip().split()]
BC.sort(key=lambda x: x[1])

res = 0
cnt = 0
i = N - 1
j = M - 1
while True:
    if cnt == N:
        break

    if j == -1:
        res += A[i]
        i -= 1
    elif A[i] > BC[j][1]:
        res += A[i]
        i -= 1
    elif A[i] <= BC[j][1]:
        res += BC[j][1]
        BC[j][0] -= 1
        if BC[j][0] == 0:
            j -= 1
    else:
        raise ValueError
    cnt += 1
print(res)
### BUGGY CODE ###

#coding: utf-8
N = int(input())
A= list(map(int,input().split()))
A = [0] + A + [0]
Adiff = list(range(N+1))
def diff(X):
    diff_x = list(range(len(X)-1))
    for i in range(len(X)-1):
        diff_x[i] = abs(X[i] - X[i+1])
    return diff_x
for i in range(N):
    B = A[:]
    del B[i+1]
    diffB = diff(B)
    SUM = 0
    for j in range(N):
        SUM += diffB[j]
    print(SUM)


### FIXED CODE ###

#coding: utf-8
N = int(input())
A= list(map(int,input().split()))
A = [0] + A + [0]
def diff(X):
    diff_x = list(range(len(X)-1))
    for i in range(len(X)-1):
        diff_x[i] = abs(X[i] - X[i+1])
    return diff_x
Adiff = diff(A)
SUM = 0
for i in range(len(Adiff)):
    SUM += Adiff[i]
for i in range(N):
    SUMM = SUM
    SUMM += abs(A[i]-A[i+2]) - Adiff[i+1]-Adiff[i]
    print(SUMM)
### BUGGY CODE ###

X,Y,A,B,C = list(map(int, input().split()))
As = sorted(list(map(int, input().split())), reverse=True)
Bs = sorted(list(map(int, input().split())), reverse=True)
Cs = sorted(list(map(int, input().split())), reverse=True)

s = 0
_C = 0
while A+B+C > 0:
  if X+Y == _C:
    break
  if X+Y == 0:
    break
  mx = 0
  mxi = None
  for i, v in enumerate([As[0] if A>0 and X>0 else 0, Bs[0] if B>0 and Y>0 else 0, Cs[0] if C>0 else 0]):
    if v > mx:
      mx = v
      mxi = i
  if mxi == 0:
    A -= 1
    X -= 1
    As = As[1:] if len(As) > 1 else []
  elif mxi == 1:
    B -= 1
    Y -= 1
    Bs = Bs[1:] if len(Bs) > 1 else []
  elif mxi == 2:
    C -= 1
    _C += 1
    Cs = Cs[1:] if len(Cs) > 1 else []
  s += mx

print(s)


### FIXED CODE ###

X,Y,A,B,C = list(map(int, input().split()))
As = sorted(list(map(int, input().split())), reverse=True)
Bs = sorted(list(map(int, input().split())), reverse=True)
Cs = list(map(int, input().split()))

As = As[:X]
Bs = Bs[:Y]
ans = 0
Xs = sorted(As+Bs+Cs, reverse=True)
print(sum(Xs[:X+Y]))
### BUGGY CODE ###


N, K=map(int,input().split())
arr = list(map(int,input().split()))
if(K>= N):
  print('0')
else:
  if (K > 0):
    while(K):
      arr.remove(max(arr))
      K -= 1;

  print(sum(arr))

### FIXED CODE ###

N, K=map(int,input().split())
arr = list(map(int,input().split()))
if(K>= N):
  print('0')
else:
  if (K > 0):
    new_arr = sorted(arr)
    arr = new_arr[:-K]
  print(sum(arr))
### BUGGY CODE ###

N = int(input())
A = [int(i) for i in input().split()]
c =1
for i in A:
  c *= i
print(c if c <= 10**18 else "-1")

### FIXED CODE ###

def main():
  N = int(input())
  A = [int(i) for i in input().split()]
  
  if 0 in A:
    print(0)
    return
  
  c = 1
  for i in A:
    c *= i
    
    if c > 10**18:
      print(-1)
      return
  print(c)
  
main()
### BUGGY CODE ###

# D - Candy Distribution

import numpy as np

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

C = A.cumsum()
C = np.insert(C,0,0)

ans = 0
for i in range(N):
    for j in range(i+1, N+1):
        if (C[j] - C[i]) % M == 0:
            ans += 1
            
print(ans)

### FIXED CODE ###

# D - Candy Distribution

import numpy as np
from collections import defaultdict

N, M = map(int, input().split())
A = np.array(list(map(int, input().split())), np.int64)

C = A.cumsum()
C = np.insert(C,0,0)
remainder = defaultdict(int)

for x in C:
    remainder[x%M] += 1

ans = 0
for key in remainder:
    if remainder[key] >= 2:
        ans += (remainder[key] * (remainder[key] - 1)) // 2

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

n, m = map(int,input().split())
ans = [0]*n

for _ in range(m):
  l, r = map(int,input().split())
  for i in range(l-1, r):
    ans[i] += 1
print(ans.count(m))

### FIXED CODE ###

n, m = map(int,input().split())
maxL, minR = 1, n
L = []
R = []
for i in range(m):
  l, r = map(int,input().split())
  maxL = max(maxL, l)
  minR = min(minR, r)

ans = minR - maxL + 1
print(max(ans, 0))