Problem:


Score : 300 points

Problem Statement

We have N ID cards, and there are M gates.

We can pass the i-th gate if we have one of the following ID cards: the L_i-th, (L_i+1)-th, ..., and R_i-th ID cards.

How many of the ID cards allow us to pass all the gates alone?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i \leq R_i \leq N

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

Print the number of ID cards that allow us to pass all the gates alone.

Sample Input 1

4 2
1 3
2 4

Sample Output 1

2

Two ID cards allow us to pass all the gates alone, as follows:

The first ID card does not allow us to pass the second gate.

The second ID card allows us to pass all the gates.

The third ID card allows us to pass all the gates.

The fourth ID card does not allow us to pass the first gate.

Sample Input 2

10 3
3 6
5 7
6 9

Sample Output 2

1

Sample Input 3

100000 1
1 100000

Sample Output 3

100000



Sample input:
4 2
1 3
2 4


Sample output:
2

Submission status: Wrong Answer

Solution code:
N, M, *_LR = map(int, open(0).read().split())
LR = list(zip(*[map(int, iter(_LR))]*2))

L_max = 1
R_min = N

for L,R in LR:
    L_max = max(L, L_max)
    R_min = min(R, R_min)
    
print(R_min - L_max + 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
lmax = 0
rmin = 1000
for _ in range(m):
    l, r = map(int, input().split())
    lmax = max(lmax, l)
    rmin = min(rmin, r)
if lmax > rmin:
    print(0)
else:
    print(rmin-lmax+1)

Submission status: Wrong Answer

Solution code:
def main():
    n, m = map(int,input().split()) 
    startlis = []
    endlis = []

    for i in range(m):
        a, b = map(int, input().split())
        startlis.append(a)
        endlis.append(b)
    
    start = max(startlis)
    end = min(endlis)

    print(end - start + 1)





    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,m = map(int, raw_input().split(' '))
def intersection(A,B):
	a,b  = max(A[0],B[0]), min(A[1],B[1])
	return [a,b] if a <= b else []

r = [1,n]
for _ in range(m):
	a,b = map(int, raw_input().split(' '))
	keys = intersection(r, [a,b])
	if len(keys) == 0: break
print (keys[1] - keys[0] + 1) if keys else 0

Submission status: Wrong Answer

Solution code:
n,m = map(int, raw_input().split(' '))
def intersection(A,B):
	a,b  = max(A[0],B[0]), min(A[1],B[1])
	return [a,b] if a <= b else []

r = [1,n+1]
for _ in range(m):
	a,b = map(int, raw_input().split(' '))
	keys = intersection(r, [a,b])
	if len(keys) == 0: break
print (keys[1] - keys[0] + 1) if keys else 0

Submission status: Wrong Answer

Solution code:
def solve(l,r):
  l_max = max(l)
  r_max = max(r)
  return max(r_max - l_max + 1, 0)
  
N,M = map(int,input().split())
L = [0]*M
R = [0]*M
for i in range(M):
  L[i],R[i] = map(int,input().split())
print(solve(L,R))

Submission status: Wrong Answer

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: map(str, sys.stdin.buffer.readline().split())
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n, m = rm()
l = 0
r = inf
for _ in range(m):
  a, b = rm()
  l = max(a, l)
  r = min(b, r)
c = r-l+1
if c:
  print(c)
else:
  print(0)

Submission status: Wrong Answer

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: map(str, sys.stdin.buffer.readline().split())
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n, m = rm()
l = 0
r = inf
for _ in range(m):
  a, b = rm()
  l = max(a, l)
  r = min(b, r)
print(r-l+1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
L = []
R = []
for i in range(M):
  l, r = map(int, input().split())
  L.append(l)
  R.append(r)

print(min(R) - max(L) + 1)

Submission status: Wrong Answer

Solution code:
def resolve():
    N, M = list(map(int, input().split()))
    interval = []
    for i in range(M):
        L, R = list(map(int, input().split()))
        interval.append(range(L + 1, R + 1))
    tmp = set(interval[0])
    for j in interval:
        tmp = set(j) & tmp
    print(len(set(range(1, N + 1)) & tmp)+1)

    return

resolve()

Submission status: Wrong Answer

Solution code:
def i():
	return int(input())
def i2():
	return map(int,input().split())
def s():
	return str(input())
def l():
	return list(input())
def intl():
	return list(int(k) for k in input().split())

n,m = i2()
l = []
r = []

for i in range(m):
	L,R = i2()
	l.append(L)
	r.append(R)

print(min(r) - max(l) + 1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
L = []
R = []
for i in range(M):
    l, r = map(int, input().split())
    L.append(l)
    R.append(r)
print(min(R) - max(L) + 1)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
card =[list(map(int,input().split())) for i in range(m)]
r = card[0][0]
R = card[0][1]
for i in range(m):
    if  r < card[i][0]:
        r = card[i][0]
for i in range(m):
    if card[i][1] < R:
        R = card[i][1]

print(R - r + 1)

Submission status: Wrong Answer

Solution code:
def main():
    N,M = map(int, input().split())
    l=-100000
    r=1000000
    for i in range(M):
        L,R =map(int,input().split())
        if l<L:
            l=L
        if R<r:
            r=R
    print(r-l+1)
main()

Submission status: Wrong Answer

Solution code:
n,m = map( int,input().split() )
a = [ list(map(int,input().split())) for _ in range(m)]

L = a[0][0]
R = a[0][1]

for i in range(1,m):
  L = max(L,a[i][0])
  R = min(R,a[i][1])

R = min(R,n)

print(R - L + 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
l = [0]*m
r = [0]*m
for i in range(m):
    l[i], r[i] = map(int, input().split())
l_max = max(l)
r_min = min(r)

print(r_min-l_max+1)

Submission status: Wrong Answer

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
# from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx as nx
# from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
n,m=n1()
LR=n3(m)

m=0
M=10**10
for l,r in LR:
    m=max(m,l)
    M=min(M,r)

print(M-m+1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
mini = 0
maxi = N
for i in range(M):
  L, R = map(int, input().split())
  mini = max(mini, L)
  maxi = min(maxi, R)
print(maxi - mini + 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
apple = []
for _ in range(m):
    apple.append(list(map(int, input().split())))
cat = []
dog = []
for i in apple:
    cat.append(i[0])
    dog.append(i[1])
print(min(dog) - max(cat) + 1)

Submission status: Wrong Answer

Solution code:
#C
from collections import deque
n,m = map(int,input().split())
gate = deque()
for _ in range(m):
    l,r = map(int,input().split())
    if len(gate) == 0:
        gate.append([l,r])
        #print(gate)
    else:
        check = gate.pop()
        #print(gate)
        if check[0] < l:
            check[0] = l
            #print(check[0])
        if check[1] > r:
            check[1] = r
        gate.append(check)
     
result = gate.pop()
print(result[1]-result[0]+1)

Submission status: Wrong Answer

Solution code:
import numpy as np
def solve():
    N,M = map(int, input().split())
    LR = np.array([list(map(int, input().split())) for _ in range(M)])
    Lmax = np.max(LR[:,0])
    Rmin = np.min(LR[:,1])
    print(Rmin - Lmax + 1)





if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
L = []
R = []
for i in range(m):
    l, r = map(int, input().split())
    L.append(l)
    R.append(r)
print(min(R) - max(L) + 1)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
l=[]
r=[]
for i in range(m):
    strl,strr=map(int,input().split())
    l.append(strl)
    r.append(strr)
num1=max(l)
num2=min(r)
if num1>=num2:
    print(0)
else:
    print(num2-num1+1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
l, r = [], []
for _ in range(m):
    li, ri = map(int, input().split())
    l.append(li)
    r.append(ri)
print(min(r) - max(l) + 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
l, r = 0, n
for _ in range(m):
    li, ri = map(int, input().split())
    l = max(l, li)
    r = min(r, ri)
print(r - l + 1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
gate = [input().split() for i in range(M)]

MIN = 0
MAX = float('inf')

for i in range(M):
    MIN = max(MIN, int(gate[i][0]))
    MAX = min(MAX, int(gate[i][1]))
    print(MIN, MAX)

print(max(MAX - MIN + 1, 0))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
gate = [input().split() for i in range(M)]

MIN = 0
MAX = float('inf')

for i in range(M):
    MIN = max(MIN, int(gate[i][0]))
    MAX = min(MAX, int(gate[i][1]))

print(MAX - MIN + 1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())

c_min = 1
c_max = 10 ** 5
for i in range(M):
    LM = list(map(int, input().split()))
    c_min = max(c_min, LM[0])
    c_max = min(c_max, LM[1])

print(c_max - c_min + 1)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
ls = [i for i in range(m)]
rs = [i for i in range(m)]
cnt = 0
for _ in range(m):
    ls[_],rs[_] = map(int,input().split())

maxl = max(ls)
minr = min(rs)

for nn in range(1,n+1):
    if maxl <= nn or minr >= nn:
        cnt += 1

print(cnt)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())

max_L = []
min_R = []

cnt = 0
for i in range(m):
    L,R = map(int,input().split())
    max_L.append(L)
    min_R.append(R)
print(min(min_R)-max(max_L)+1)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())

num_conter = [1]*n

max_L = 0
max_R = 10**5+1

for i in range(m):
    L,R = map(int,input().split())

    if max_L < L:
        max_L = L
    if max_R > R:
        max_R = R
print(max_R-max_L+1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
Lmax = 0
Rmin = 10**5 + 1
for n in range(M):
  L, R = map(int, input().split())
  if L > Lmax:
    Lmax = L
  if R < Rmin:
    Rmin = R

gate = Rmin - Lmax + 1
print(gate)

Submission status: Wrong Answer

Solution code:
def main():
    n, m = map(int, input().split())
    l = [list(map(int, input().split())) for i in range(m)]
    lt = [list(x) for x in zip(*l)]
    if min(lt[1]) - max(lt[0]):
        print(min(lt[1]) - max(lt[0]) + 1)
    else:
        print(0)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, m = map(int, input().split())
    l = [list(map(int, input().split())) for i in range(m)]
    lt = [list(x) for x in zip(*l)]
    print(min(lt[1]) - max(lt[0]) + 1)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, m = map(int, input().split())
    l = [list(map(int, input().split())) for i in range(m)]
    res = list(range(l[0][0], l[0][1] + 1)) 
    if m > 1:
        for i in range(1, m):
            res = list(set(list(range(l[i][0], l[i][1] + 1))) & set(res))
        print(len(res))
    else:
        if n == l[0][1] and l[0][0] == 1:
            print(n)
        else:
            print(0)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import Counter
import numpy as np

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

for i in range(m):
    data[i] = list(map(int, input().split()))


data = np.array(data)
ll = data[:, 0]
rr = data[:, 1]

ans = np.min(rr) - max(ll) + 1
print(ans)

'''
n, m = map(int, input().split())
tar = [0] * (1 + 10 ** 5)
tar = np.array(tar)

for i in range(m):
    l, r = map(int, input().split())
    tar[l:r + 1] += 1
    #print(tar[:10])

#tar = tar.tolist()
# print(tar[0:5])
count = Counter(tar)
ans = count[m]
print(ans)
#-----------------------------
n, m = map(int, input().split())

ll, rr = map(int, input().split())
for i in range(m - 1):
    l, r = map(int, input().split())

    #lについて
    if (l <= ll):
      pass
    elif (ll < l )

ans = max(ll - rr, 0)
print(ans)

'''

Submission status: Wrong Answer

Solution code:
from collections import Counter
import numpy as np

n, m = map(int, input().split())
tar = [0] * (1 + 10 ** 5)
tar = np.array(tar)

for i in range(m):
    l, r = map(int, input().split())
    tar[l:r + 1] += 1
    print(tar[:10])

#tar = tar.tolist()
# print(tar[0:5])
count = Counter(tar)
ans = count[m]
print(ans)
'''
n, m = map(int, input().split())

ll, rr = map(int, input().split())
for i in range(m - 1):
    l, r = map(int, input().split())

    #lについて
    if (l <= ll):
      pass
    elif (ll < l )

ans = max(ll - rr, 0)
print(ans)

'''

Submission status: Wrong Answer

Solution code:
n,m=list(map(int,input().split()))
l=0
r=10**5+1
for i in range(m):
  ll,rr=list(map(int,input().split()))
  l=max(l,ll)
  r=min(r,rr)
print(r-l+1)

Submission status: Wrong Answer

Solution code:
n,m=list(map(int,input().split()))
l=0
r=10**5
for i in range(m):
  ll,rr=list(map(int,input().split()))
  l=max(l,ll)
  r=min(r,rr)
print(r-l+1)

Submission status: Wrong Answer

Solution code:
def resolve():
    N, M = map(int, input().split())

    start_index = 1
    last_index = N
    for _ in range(M):
        pp, ss = map(int, input().split())
        start_index = max(start_index, pp)
        last_index = min(last_index, ss)

    print(last_index - start_index + 1)


resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    N, M = map(int, input().split())

    start_index = 0
    last_index = N
    for _ in range(M):
        pp, ss = map(int, input().split())
        start_index = max(start_index, pp)
        last_index = min(last_index, ss)

    print(last_index - start_index + 1)


resolve()

Submission status: Wrong Answer

Solution code:
#import numpy as np
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as comb_with

#A = int(input())
N,M= map(int,input().split())
#A = list(map(int,input().split()))
#B = list(map(int,input().split()))
#S= str(input())
left=1
right=N
for m in range(M):
    l,r = map(int,input().split())
    if left<=l and l<=right:
        left=l
    if left<=r and r<=right:
        right=r

print(right-left+1)

Submission status: Wrong Answer

Solution code:
#import numpy as np
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as comb_with

#A = int(input())
N,M= map(int,input().split())
#A = list(map(int,input().split()))
#B = list(map(int,input().split()))
#S= str(input())
left=1
right=10**6
for m in range(M):
    l,r = map(int,input().split())
    if left<=l and l<=right:
        left=l
    if left<=r and r<=right:
        right=r

print(right-left+1)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
mn=0
mx=N
for i in range(M):
 L,R=map(int,input().split())
 mn=max(L,mn)
 mx=min(R,mx)
print(mx-mn+1)

Submission status: Wrong Answer

Solution code:
N, M=map(int, input().split())  #複数数値入力　「A B」みたいなスペース空いた入力のとき
LR=[[int(i) for i in input().split()] for i in range(M)]  # 二次元配列入力　二次元マップみたいな入力のとき

mi, ma = 0, 10**5

for i in LR:
    mi = max(mi, i[0])
    ma = min(ma, i[1])

if ma-mi <= 0:
    print(0)
else:
    print(ma-mi + 1)

Submission status: Wrong Answer

Solution code:

N, M=map(int, input().split())  #複数数値入力　「A B」みたいなスペース空いた入力のとき
LR=[[int(i) for i in input().split()] for i in range(M)]  # 二次元配列入力　二次元マップみたいな入力のとき

mi, ma = 0, 10**5

for i in LR:
    mi = max(mi, i[0])
    ma = min(ma, i[1])

print(ma-mi + 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
Max = 100000
Min = 0

for _ in range(m):
  a, b = map(int, input().split())
  if Max > b:
    Max = b
  elif Min < a:
    Min = a

if Max-Min >= 0:
  print(Max-Min+1)
else:
  print(0)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
Max = 100000
Min = 0

for _ in range(m):
  a, b = map(int, input().split())
  if Max > b:
    Max = b
  elif Min < a:
    Min = a
  
print(Max-Min+1)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())

l_max=1
r_min=n

for i in range(m):
    l,r=map(int,input().split())
    l_max=max(l_max,l)
    r_min=min(r_min,r)
print(r_min-l_max+1)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
L = [0]*m
R = [0]*m

max_l = 1
min_r = n
for i in range(m):
    l,r = map(int,input().split())
    max_l = max(max_l,l)
    min_r = min(min_r,r)
print(min_r-max_l+1)

Submission status: Wrong Answer

Solution code:
N, M = list(map(int, input().split()))
L = []
R = []
for _ in range(M):
    l, r = list(map(int, input().split()))
    L += [l]
    R += [r]
print(min(R) - max(L) + 1)

Submission status: Wrong Answer

Solution code:
N, M = map(int,input().split())


LR = [list(map(int,input().split())) for _ in range(M)]

max_L = 0
min_R = 10**5

for m in range(M):
  if max_L > LR[m][0]:
    max_L = LR[m][0]
  if min_R <LR[m][1]:
    min_R = LR[m][1]
    

L = [x[0] for x in LR]
R = [x[1] for x in LR]

if max(L) - min(R) >=0:
  print(max(L)-min(R)+1)
else:
  print(0)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
ansl=1
ansr=n
for i in range(m):
    l,r=map(int,input().split())
    if ansl<l:
        ansl=l
    if ansr>r:
        ansr=r
print(ansr-ansl+1)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
ansl=0
ansr=n+1
for i in range(m):
    l,r=map(int,input().split())
    if ansl<l:
        ansl=l
    if ansr>r:
        ansr=r
print(ansr-ansl+1)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
LR=[list(map(int, input().split())) for _ in range(M)]
left=0
right=N+1
ans=0
for i in range(M):
	left=max(left,LR[i][0])
	right=min(right,LR[i][1])
ans=right-left+1
print(ans)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
l=[list(map(int,input().split()))for i in range(M)]
min=0
max=1000000
for i in l:
    if i[0]>=min:
        min=i[0]
    if i[1]<=max:
        max=i[1]
print(max-min+1)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
a = 1
b = n
for i in range(m):
    l,r = map(int,input().split())
    a = max(a,l)
    b = min(b,r)
print(b-a+1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
cnt = [0] * (n + 2)
cnt_sum = [0] * (n + 1)
for _ in range(m):
    l, r = map(int, input().split())
    cnt[l] += 1
    cnt[r + 1] -= 1
cnt_sum[0] = cnt[0]
for i in range(1, n + 1):
    cnt_sum[i] = cnt_sum[i - 1] + cnt[i]
print(cnt_sum.count(max(cnt_sum[1:])))

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
cnt = [0] * (n + 2)
cnt_sum = [0] * (n + 1)
for _ in range(m):
    l, r = map(int, input().split())
    cnt[l] += 1
    cnt[r + 1] -= 1
for i in range(1, n + 1):
    cnt_sum[i] = cnt_sum[i - 1] + cnt[i]
print(cnt_sum.count(max(cnt_sum[1:])))

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
cnt = [0] * (n + 3)
cnt_sum = [0] * (n + 2)
for _ in range(m):
    l, r = map(int, input().split())
    cnt[l] += 1
    cnt[r + 1] -= 1
for i in range(1, n + 2):
    cnt_sum[i] = cnt_sum[i - 1] + cnt[i]
print(cnt_sum.count(max(cnt_sum)))

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
cnt = [0] * (n + 3)
cnt_sum = [0] * (n + 1)
for _ in range(m):
    l, r = map(int, input().split())
    cnt[l] += 1
    cnt[r + 1] -= 1
for i in range(1, n + 1):
    cnt_sum[i] = cnt_sum[i - 1] + cnt[i]
print(cnt_sum.count(max(cnt_sum)))

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
L = [0]*M
R = [0]*M

for i in range(M):
    L[i],R[i] = map(int,input().split())
    
L.sort()
R.sort()

l= L[len(L)-1]
r = R[0]

cnt = r-l+1

print(cnt)

Submission status: Wrong Answer

Solution code:
(N, M), *LR, = [map(int, s.split()) for s in open(0)]
card_l = 1
card_r = N
for l, r in LR:
    card_l = max(l, card_l)
    card_r = min(r, card_r)
print(card_r - card_l + 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int,input().split())
l = []
r = []
for i in range(m):
    a, b = map(int,input().split())
    l.append(a)
    r.append(b)

a = 1
b = n
for i in range(m):
    a = max(l[i], a)
    b = min(r[i], b)

print(b - a + 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int,input().split())
l = []
r = []
for i in range(m):
    a, b = map(int,input().split())
    l.append(a)
    r.append(b)

te = max(l)
se = max(r)
maxv = max(te, se)

te = min(l)
se = min(r)
minv = min(te, se)

a = minv
b = maxv
for i in range(m):
    a = max(l[i], a)
    b = min(r[i], b)

print(b - a + 1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
a = [0]*N
for i in range(M):
    L, R = map(int, input().split())
    a[:L-1] = [1]*L
    a[R:] = [1]*(N-R+1)
count = 0
for b in a:
    if b ==0:
        count+=1
print(count)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
def main():
    n, m = map(int, readline().split())
    INF = int(1e5)+1
    mx = -1
    mi = INF 
    for _ in range(m):
        l, r = map(int, readline().split())
        mx = max(l, mx)
        mi = min(r, mi)
    print(mi-mx+1)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
a = [0]*N
for i in range(M):
    L, R = map(int, input().split())
    for b in range(R-L+1):
        a[L+b-1] += 1
count = 0
for c in a:
    if c == 3:
        count+=1
print(count)

Submission status: Wrong Answer

Solution code:
n, m = map(int,input().split())
l = []
r = []
for i in range(m):
    a, b = map(int,input().split())
    l.append(a)
    r.append(b)

te = max(l)
se = max(r)
maxv = max(te, se)

te = min(l)
se = min(r)
minv = min(te, se)

val = [i for i in range(minv, maxv+1)]

t = set()
for i in range(m):
    part = val[l[i]-1:r[i]]
    if i == 0:
        part = set(part)
        t = part
    else:
        t = t & set(part)

print(len(t))

Submission status: Wrong Answer

Solution code:
"""
i番目のゲートは LiからRiまでのIDカードのうちどれか1枚を持っていれば通過できます
"""

N, M = map(int, input().split())
left = 0
right =N

for i in range(M):
    L, R = map(int, input().split())
    if left<L:
        left = L
    if right > R:
        right = R

print(right-left+1)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
low=1
high=n
for i in range(m):
  l,r=map(int,input().split())
  if l>low:
    low=l
  if r<high:
    high=r
  print(low,high)
if high>=low:
  print(high-low+1)
else:
  print(0)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
li,lia = [],[]
for i in range(m):
    a,b = map(int,input().split())
    li.append(a)
    lia.append(b)
print(min(lia)-max(li)+1)

Submission status: Wrong Answer

Solution code:
def resolve():
    N, M = [int(i) for i in input().split()]
    llrr = [list(map(int, input().split())) for _ in range(M)]
    maxL = 1
    minR = N
    for l, r in llrr:
        maxL = max(maxL, l)
        minR = min(minR, r)
    ans = minR - maxL + 1
    if ans < 0:
        print(0)
    else:
        print(ans)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]
l = 0
r = 100001
for lr in LR:
  l = max(lr[0], l)
  r = min(r, lr[1])
  
print(r - l + 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
l, r = [0]*m, [0]*m
for i in range(m):
    l[i], r[i] = map(int, input().split())

ll = max(l)
rr = min(r)
print(rr-ll+1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())

L = 1
R = 10**5

for i in range(m):
    l, r = map(int, input().split())
    if L < l < R:
        L = l
    if L < r < R:
        R = r

print(R - L + 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())

L = 1
R = 10**5

for i in range(m):
    l, r = map(int, input().split())
    if L < l:
        L = l
    if r < R:
        R = r

print(R - L + 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())

L = 1
R = 10**5

for i in range(m):
    l, r = map(int, input().split())
    L = max(L, l)
    R = min(R, r)

print(R - L + 1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
 
left, right = 1, N
 
for _ in range(M):
  L, R = map(int, input().split())
  
  left = max(L, left)
  right = min(R, right)
  
print(right - left + 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
ll = [0 for i in range(n+1)]
l_max = 0
r_min = 1000000
for _ in range(m):
    l, r = map(int, input().split())
    if l > l_max :
        l_max = l
    if r < r_min:
        r_min = r
print(r_min - l_max + 1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

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

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

N,M = IL()

L = 0
R = INF

for i in range(M):
  l,r = IL()
  L = max(L,l)
  R = min(R,r)
  
print(R-L+1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
Lmax = 0
Rmin = n
for _ in range(m):
  L, R = map(int, input().split())
  Lmax = max(L, Lmax)
  Rmin = min(R, Rmin)
  
print(Rmin - Lmax + 1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
limL = 1
limR = N
for i in range(M):
  L, R = map(int, input().split())
  limL = max([limL, L])
  limR = min([limR, R])

print(limR-limL+1)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
l = [0]*m
r = [0]*m
for i in range(m):
    l[i],r[i] = map(int,input().split())
if min(r)-max(l) < 0:
    print(0)
else:
    print(min(r)-max(l))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
gates = []
for _ in range(M):
    gates.append(tuple(map(int, input().split())))

L = 1
R = N
for l, r in gates:
    if l > L:
        L = l
    if r < R:
        R = r

print(R - L + 1)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
ans=0
L,R=0,(10**5)+1
for i in range(M):
    l,r=map(int,input().split())
    L=max(L,l); R=min(R,r)
    if r<l: break
R=min(R,N)
print(abs((L-1)-R) if 0<R-L else 0)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
ans=0
L,R=0,(10**5)+1
for i in range(M):
    l,r=map(int,input().split())
    L=max(L,l); R=min(R,r)
    if r<l: break
R=min(R,N)
print(abs((L-1)-R))

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
ans=0
L,R=0,10**5
for i in range(M):
    l,r=map(int,input().split())
    L=max(L,l); R=min(R,r)
R=min(R,N)
print(abs((L-1)-R))

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
ans=0
L,R=0,10**5
for i in range(M):
    l,r=map(int,input().split())
    L=max(L,l); R=min(R,r)
print(L,R)
R=min(R,N)
print(abs((L-1)-R))

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
ans=0
L,R=0,10**5
for i in range(M):
    l,r=map(int,input().split())
    L=max(L,l); R=min(R,r)
L=max(L,1); R=min(R,N)
print(abs(L-R)+1)

Submission status: Wrong Answer

Solution code:
n,m = map(int, input().split())
fn = 1
ln = n
for i in range(m):
    l,r = map(int, input().split())
    fn = max(fn,l)
    ln = min(ln,r)    
print(ln-fn+1 if ln-fn>0 else 0)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
gate = [0] * N
maxL, minR = 0, N
for i in range(M):
    L, R = map(int, input().split())
    maxL = max(maxL, L-1)
    minR = min(minR, R-1)

count = minR - maxL + 1
print(count)