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 = list(map(int, input().split()))
for i in range(M):
    if i==0:
        L1, R1 = list(map(int, input().split()))
    else:
        L2, R2 = list(map(int, input().split()))
        if R1 < L2 or L1 > R2:
            print(0)
            break
        elif L2<L1 and R1<R2:
            continue
        elif L1<L2 and R2<R1:
            L1 = L2
            R1 = R2
        elif L2<L1 and R2<R1:
            R1 = R2
        elif L1<L2 and R1<R2:
            L1 = L2
        else:
            continue
s = set(range(L1,R1+1))
print(len(s))

Submission status: Wrong Answer

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

lmin = 0
rmax = n
for _ in range(m):
    l,r = map(int,input().split())
    if l > lmin:
        lmin = l
    if rmax > r:
        rmax = r
    
print(rmax - lmin + 1)

Submission status: Wrong Answer

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

keys = set()
for l, r in LR:
    cur = set()
    for i in range(l, r+1):
        cur.add(i)
    if len(keys) == 0:
        keys = cur
    else:
        keys = keys & cur

print(len(keys))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

min_id = 1
max_id = N
for m in range(M):
    left = LR[m][0]
    right = LR[m][1]
    if min_id < left:
        min_id = left
    if right < max_id:
        max_id = right

print(max_id - min_id + 1)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
mat = [list(map(int,input().split())) for i in range(M)]
L = 0
R = 10**5+1
for i in range(M):
    if L < mat[i][0]:
        L = mat[i][0]
    if R > mat[i][1]:
        R = mat[i][1]
print(R-L+1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,m=map(int,input().split())
    L=-INF; R=INF
    for _ in range(m):
        l,r=map(int,input().split())
        L=max(L,l)
        R=min(R,r)
    print(R-L+1)
resolve()

Submission status: Wrong Answer

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

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_min = L[0]
R_max = R[0]
for i in range(1, M):
    if L_min < L[i]:
        L_min = L[i]
    if R[i] < R_max:
        R_max = R[i]
print(R_max-L_min+1)

Submission status: Wrong Answer

Solution code:
N,R = map(int,input().split())
min = 0
max = 10000000
ans = 0
for i in range(R):
  A,B = map(int,input().split())
  if A > min:
    min = A
  if B < max:
    max = B
if min < max:
  ans = max - min + 1
print(ans)

Submission status: Wrong Answer

Solution code:
N,R = map(int,input().split())
min = 0
max = 10000000
ans = 0
for i in range(R):
  A,B = map(int,input().split())
  if A > min:
    min = A
  if B < max:
    max = B
ans = max - min + 1
print(ans)

Submission status: Wrong Answer

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

for j in range(m):
  num1 = max(num[j][0], num1)
  num2 = min(num[j][1], num2)

print(num2 - num1 + 1)

Submission status: Wrong Answer

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

for i in range(n):
  ans = 0
  for j in range(m):
    if i >= num[j][0] and i <= num[j][1]:
      ans += 1
    else:
      break
  if ans == m:
    count += 1

print(count)

Submission status: Wrong Answer

Solution code:
n, m = [int(i) for i in input().split()]
lr = [[int(i) for i in input().split()] for j in range(m)]
l = max(x[0] for x in lr)
r = min(x[1] for x in lr)
print(r - l + 1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
l=0
r=10**5
for _ 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())
l = 0
r = n+1
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())
for i in range(m):
    l_, r_ = map(int, input().split())
    if i == 0:
        l = l_
        r = r_
    else:
        l = max(l, l_)
        r = min(r, r_)

print(r-l+1)

Submission status: Wrong Answer

Solution code:
[n,m]=map(int,raw_input().split())
l,r=1,n
for i in range(m):
	[li,ri]=map(int,raw_input().split())
	l = max(li, l)
	r = min(ri, r)
ans=l-r+l
print(ans if ans > 0 else 0)

Submission status: Wrong Answer

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

max_L, min_R = (0, N)

for r in l:
    if r[0]>max_L:
        max_L = r[0]
    if r[1]<min_R:
        min_R = r[1]
if min_R > max_L:
    print(min_R - max_L + 1)
else:
    print(0)

Submission status: Wrong Answer

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

max_L, min_R = (0, N)

for r in l:
    if r[0]>max_L:
        max_L = r[0]
    if r[1]<min_R:
        min_R = r[1]
print(min_R - max_L + 1)

Submission status: Wrong Answer

Solution code:
def main():
    n,m = [int(x) for x in input().split()]

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

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

l = [-1]*m
r = [-1]*m
for i in range(m):
  l[i], r[i] = map(int, input().split())


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

Submission status: Wrong Answer

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

maxl = 1
minr = N
for i in range(0,M):
  l,r = map(int,input().split())
  if l > maxl:
    maxl = l
  if r < minr:
    minr = r
print(minr-maxl+1)
#print(maxl,minr)

Submission status: Wrong Answer

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

min_val_list = []
max_val_list = []
for i in range(M):
    min_id_range, max_id_range = map(int, input().split())
    min_val_list.append(min_id_range)
    max_val_list.append(max_id_range)

min_val_id = max(min_val_list)
max_val_id = min(max_val_list)


print(max_val_id - min_val_id + 1)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000)
INF = 1<<32


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    
    lm = max(L)
    rm = min(R)

    print(rm-lm+1)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    L = [int()] * (M)  # type: "List[int]"
    R = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))
    solve(N, M, L, R)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,m = map(int, input().split())
gate = [0 for i in range(n+1)]
for i in range(m):
  l,r = map(int, input().split())
  for j in range(l,r+1):
    gate[j] += 1
print(max(gate))
print(gate)
if max(gate) < m:
  print(0)
else:
  print(gate.count(m))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
ll = 0
rr = 10**5+1
for i in range(m):
    l,r = map(int,input().split())
    if l > ll:
        ll = l
    if r < rr:
        rr = r
else:
    #print(rr-ll+1)
    if rr-ll+1>n:
        print(n)
    else:
        print(rr-ll+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    result = min(R) - max(L) + 1
    print(result)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    L = [int()] * (M)  # type: "List[int]"
    R = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))
    solve(N, M, L, R)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

for _ in range(M):
  L ,R = map(int, input().split())
  for i in range(L-1,R):
      card[i] = 1
      
print(card.count(1))

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
Key=[0]*(N+1)
for i in range(M):
    L,R = map(int,input().split())
    Key[L-1] += 1
    Key[R] -= 1
for i in range(N):
    Key[i+1] += Key[i] # 累積和
print(Key)
print(Key.count(M))

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
Key=[0]*(N+1)
for i in range(M):
    L,R = map(int,input().split())
    Key[L-1] += 1
    Key[R] -= 1
for i in range(N):
    Key[i+1] += Key[i] # 累積和
print(Key.count(max(Key)))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
d=[]
if m==1:
  print(n)
  m=0
if m!=0:
  for i in range(m):
    L,R=map(int,input().split())
    if i==0:
      d.append(R)
    if i==(m-1):
      d.append(L)
  print(len(set(d)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

inf = []
sup = []

for i in range(0,M):
	L,R = map(int,input().split())
	inf.append(L)
	sup.append(R)
	
inf = max(inf)
sup = min(sup)

num = sup - inf + 1

print(num)

Submission status: Wrong Answer

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

mi = 0
ma = 100001

for i in range(m):
	if lr[i][0] > mi:
		mi = lr[i][0]
	if lr[i][1] < ma:
		ma = lr[i][1]

print(ma-mi+1)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
mi,mx=0,n
for item in l:
  mi1=item[0]
  mx1=item[1]
  if mi1>mi:
    mi=mi1
  if mx1<mx:
    mx=mx1
print(mx-mi+1)

Submission status: Wrong Answer

Solution code:
n , m = map(int,input().split())
a , b = 1 , 100000

for i in range(m):
    l , r = map(int,input().split())
    if a <= l:
        a = l
    if b >= r:
        b = r
        
print(b - a + 1)

Submission status: Wrong Answer

Solution code:
n , m = map(int,input().split())
a , b = 1 , 10000

for i in range(m):
    l , r = map(int,input().split())
    if a <= l:
        a = l
    if b >= r:
        b = r
        
print(b - a + 1)

Submission status: Wrong Answer

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

card = [0 for i in range(n+1)]
for i in range(m):
    l, r = map(int, input().split())
    card[l-1] += 1
    card[r] -= 1

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

print(card.count(max(card)))

Submission status: Wrong Answer

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

for _ 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 _ in range(M):
    l, r = map(int, input().split())
    L.append(l)
    R.append(r)
print(max(L)-min(R)+1)

Submission status: Wrong Answer

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

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

min = 1
max = N

for i in range(M):
    if L_R_list[i][0] >= min:
        min = L_R_list[i][0]
    if L_R_list[i][1] <= max:
        max = L_R_list[i][1]

print(max-min+1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
cnt = 0
LR = [list(map(int, input().split())) for _ in range(m)]
l = []
r = []
for i in range(m):
    l.append(LR[i][0])
    r.append(LR[i][1])

l_max = max(l)
r_min = min(r)

for k in range(n):
    if l_max <= k <= r_min:
        cnt += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
cnt = 0
LR = [list(map(int, input().split())) for _ in range(m)]
for i in range(n):
    all_c = 0
    for k in range(m):
        if LR[k][0] <= i <= LR[k][1]:
            all_c += 1
    if all_c == m:
        cnt += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
import numpy as np
[n,m]=[int(x) for x in input().split()]
LR_T=np.array([[int(x)-1 for x in input().split()] for _ in range(m)]).T

print(np.min(LR_T[1])-np.max(LR_T[0])+1)

Submission status: Wrong Answer

Solution code:
import sys

N, M = map(int,sys.stdin.readline().split())
L = 1
R = N

for _ in range(M):
  l, r = map(int, sys.stdin.readline().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())
LR = [list(map(int, input().split())) for _ in range(M)]
min_n = 0
max_n = 10**5

for i in range(M):
    min_n = max(min_n, LR[i][0])
    max_n = min(max_n, LR[i][1])

print(max_n-min_n+1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
max_L = 0
min_R = 1000000
for i in range(M):
    L, R = map(int, input().split())
    if max_L < L:
        max_L = L
    if min_R > R:
        min_R = R
    
print(min_R - max_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,r = map(int,input().split())
    L[i] = l
    R[i] = r
print(min(R)-max(L)+1)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
minx,maxy = map(int,input().split())
for i in range(M-1):
    x,y = map(int,input().split())
    if minx<x:
        minx = x
    if maxy>y:
        maxy = y
if minx>N: 
    print(0)
elif maxy>N:
    print(N-minx+1)
else:
    print(maxy-minx+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,r=map(int,input().split())
    L[i]=l
    R[i]=r
    
print(min(R)-max(L)+1)

Submission status: Wrong Answer

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

max_l=0
min_r=float('inf')

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=map(int,input().split())

ID=[0]*n

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

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
L = []
R = []
for _ in range(M):
    inp = input().split()
    L.append(int(inp[0]))
    R.append(int(inp[1]))
L_max = max(L)
R_min = min(R)
print(min(R_min,N) + 1 - L_max)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
L,R = map(int,input().split())
tmp = list(range(L,R+1))
ans = []
for i in range(M-1):
  L,R = map(int,input().split())
  ans = []
  for j in tmp:
    if L<=j<=R:
      ans.append(j)
  tmp = ans
if ans == []:
  print(N)
else:
  print(len(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
n,m = map(int,input().split())
right = n
left = 0

for i in range(m):
    l,r = map(int,input().split())
    left = max(left,l)
    right = min(right,r)
if left == right:
    print(0)
else:
    print(right-left+1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
n,m = map(int,input().split())
right = n
left = 0

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

print(right-left+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for _ 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())
lr = [list(map(int,input().split())) for _ in range(m)]
s = {i:0 for i in range(1,n+1)}
for i in range(m):
    for j in range(lr[i][0],lr[i][1]+1):
        s[j] += 1
for k,v in s.items():
    if v == m:
        print(k)

Submission status: Wrong Answer

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

left = []
right = []
for i in range(m):
    a, b = map(int, input().split())
    left.append(a)
    right.append(b)

if min(right) > max(left):
    print(min(right) - max(left) + 1)
else:
    print(0)

Submission status: Wrong Answer

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

left = []
right = []
for i in range(m):
    a, b = map(int, input().split())
    left.append(a)
    right.append(b)
print(min(right) - max(left) + 1)

Submission status: Wrong Answer

Solution code:
N , M = map(int ,input().split())
left = 1
right =  N
for i in range(M):
    tmp_left , tmp_right = map(int ,input().split())
    if left < tmp_left:
        left = tmp_left
    if right > tmp_right:
        right = tmp_right
print(right - left + 1)

Submission status: Wrong Answer

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

# Lの最大値以上、Rの最小値以下の整数の個数が答え
print(min(R)-max(L)+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

n,m = map(int,input().split())
lr = [ list(map(int,input().split())) for _ in range(m) ]
# print(lr)

l = []
r = []
for i in range(m):
    l.append(lr[i][0])
    r.append(lr[i][1])
# print(l)
# print(r)
l_max = max(l)
r_min = min(r)
ans = r_min - l_max + 1
print(ans)

Submission status: Wrong Answer

Solution code:
N, M = map(int,input().split())
li = [list(map(int,input().split())) for i in range(M)]
L = {li[i][0] for i in range(len(li)) }
R = {li[i][1] for i in range(len(li)) }


mL = max(L)
mR = min(R)

ans = mR - mL + 1
print(ans)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
l, r = [], []
for _ 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):
  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())
lst=[[] for _ in range(M)]
dif=10**19
v1,v2=map(int,input().split())
for i in range(M-1):
  v_1,v_2=map(int,input().split())
  v1,v2=max(v1,v_1),min(v2,v_2)
print(v2-v1+1)

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
lmax = 0
rmin = 1000000
for i in range(b):
  l, r = map(int, input().split())
  lmax=max(l,lmax)
  rmin=min(r,rmin)
#rmin = min(a,rmin)
ans = rmin - lmax +1
print(ans)

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
lmax = 0
rmin = 100000
for i in range(b):
  l, r = map(int, input().split())
  lmax=max(l,lmax)
  rmin=min(r,rmin)
rmin = min(a,rmin)
ans = rmin - lmax +1
print(ans)

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
lmax = 0
rmin = 100000
for i in range(b):
  l, r = map(int, input().split())
  lmax=max(l,lmax)
  rmin=min(r,rmin)
ans = rmin - lmax +1
print(ans)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
L =[]
R =[]
for _ in range(M):
    L_,R_ = map(int,input().split())
    L.append(L_)
    R.append(R_)
tmp = [i for i in range(N)]
num = set(tmp)
for i in range(M):
    tmp = [j for j in range(L[i],R[i]+1)]
    num &= set(tmp)
print(len(num))

Submission status: Wrong Answer

Solution code:
n,m = (int(x) for x in input().split())
l,r = (int(x) for x in input().split())

for i in range(m - 1):
	a,b = (int (x) for x in input().split())
	if l > r:
		continue
	if l < a:
		l = a
	if r > b:
		r = b

print(r - l + 1 if r > l else 0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

prev_l, prev_r = 1, 100000
for _ in range(m):
    l, r = map(int, input().split()) 
    prev_l, prev_r = max(l, prev_l), min(r, prev_r)

print(prev_r - prev_l + 1)

Submission status: Wrong Answer

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

prev_l, prev_r = 1, 10000
for _ in range(m):
    l, r = map(int, input().split()) 
    prev_l, prev_r = max(l, prev_l), min(r, prev_r)

print(prev_r - prev_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())
ans = 0
A = [0] * N
L_max = max(L)
R_min = min(R)
ans = R_min - L_max + 1
print(ans)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
L=[1]
R=[n]
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_largest = 0
R_min = N
for m in range(M):
    L, R = map(int, input().split())
    if L >= L_largest:
        L_largest = L
    if R <= R_min:
        R_min = R

print(R_min - L_largest + 1)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
G=[[] for _ in range(M)]
for i in range (M):
  L,R=map(int,input().split())
  G[i].append([L,R])
small=[]
for i in range(M):
  small.append(G[i][0][0])
s=max(small)
large=[]
for i in range(M):
  large.append(G[i][0][1])
l=min(large)
ans=l-s+1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

def main():
    N, M = [int(x) for x in input().split()]

    A = []
    for i in range(M):
        A.append([int(x) for x in input().split()])

    answer = A[0]
    for i in range(1, M):
        total = []
        total.extend(answer)
        total.extend(A[i])
        s_total = sorted(total)
        answer = s_total[1:3]


    print(answer[1] - answer[0] + 1)




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

ll = 1
rr = 100000

for i in range(M):
    l_next, r_next = map(int, input().split())
    if l_next >= ll:
        ll = l_next
    if r_next <= rr:
        rr = r_next

print(rr - ll + 1)

Submission status: Wrong Answer

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

ll = 1
rr = 100000

for i in range(M):
    l_next, r_next = map(int, input().split())
    if l_next > ll:
        ll = l_next
    if r_next < rr:
        rr = r_next

print(rr - ll + 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
p = []
lr = [list(map(int, input().split())) for _ in range(m)]
left = [lr[i][0] for i in range(m)]
right = [lr[i][0] for i in range(m)]
ans = max(left)-min(right)
print(ans+1 if ans >= 0 else 0)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
numSet = set(range(1,n+1))
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())

ans = 0
L, R = 0, 114514

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

print(R - L + 1)

Submission status: Wrong Answer

Solution code:
C,G=map(int,input().split())
Lmax=0
Rmin=C+1
for i in range(G):
    L,R=map(int,input().split())
    if L > Lmax :
        Lmax = L
    else:
        pass
    
    if R < Rmin :
        Rmin = R
    else:
        pass
print(Rmin-Lmax+1)

Submission status: Wrong Answer

Solution code:
#N枚のIDカードとM個のゲートがある
n,m = map(int, input().split())
L = [0]*m
R = [0]*m
#答えとなる変数の定義
ans = 0
#インフィニティ
infi = 1000000000000000
#Lの最大値とRの最小値を保管する関数
l_max =0
r_min =infi
#入力
for i in range(m):
    L[i],R[i] = map(int, input().split())
    if(l_max<L[i]):
        l_max = L[i]
        continue
    if(r_min>R[i]):
        r_min = R[i]
if(l_max<r_min):
    ans = 0
else:
    ans = (r_min - l_max)+1
        
print(ans)

Submission status: Wrong Answer

Solution code:
#N枚のIDカードとM個のゲートがある
n,m = map(int, input().split())
L = [0]*m
R = [0]*m
#答えとなる変数の定義
ans = 0
#インフィニティ
infi = 1000000000000000
#Lの最大値とRの最小値を保管する関数
l_max =0
r_min =infi
#入力
for i in range(m):
    L[i],R[i] = map(int, input().split())
    if(l_max<L[i]):
        l_max = L[i]
    if(r_min>R[i]):
        r_min = R[i]
ans = (r_min - l_max)+1
        


print(ans)

Submission status: Wrong Answer

Solution code:
import sys

#N枚のIDカードとM個のゲートがある
n,m = map(int, input().split())
L = [0]*m
R = [0]*m
#答えとなる変数の定義
ans = 0

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

#最初に通ることのできるIDカードの数
ans = R[0]- L[0]+1
for i in range(m-1):
    #次のゲートで使えなくなるIDカードの数
    if(L[i+1]>L[i]):
        ans -= L[i+1]-L[i]
    elif(R[i+1]<R[i]):
        ans -= R[i]-R[i+1]
        if(ans>=0):
            ans =0
            print(ans)
            sys.exit()
    
print(ans)

Submission status: Wrong Answer

Solution code:
#C
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)
L.sort()
R.sort()
print(R[0]-L[m-1]+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for i in range(m-1):
  l, r = map(int, input().split())
  if mini < l:
    mini = l
  if maxi > r:
    maxi = r
    
print(maxi-mini+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n,m=list(map(int, input().split() ))
Firstlis=[]
Secondlis=[]
for i in range(m):
  s=list(map(int,input().split()))
S1=s[0]
S2=s[1]
Firstlis.append(S1)
Secondlis.append(S2)
Min=max(Firstlis)
Max=min(Secondlis)
if Max>=Min:
    print(Max-Min+1)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
n,m=list(map(int, input().split() ))
Firstlis=[]
Secondlis=[]
for i in range(m):
  s=list(map(int,input().split()))
S1=s[0]
S2=s[1]
Firstlis.append(S1)
Secondlis.append(S2)
Min=max(Firstlis)
Max=min(Secondlis)
print(Max-Min+1)

Submission status: Wrong Answer

Solution code:
n,m=list(map(int, input().split() ))
Firstlis=[]
Secondlis=[]
for i in range(m):
  s=list(map(int,input().split()))
S1=s[0]
S2=s[1]
Firstlis.append(S1)
Secondlis.append(S2)
Min=max(Firstlis)
Max=min(Secondlis)
print(Max-Min)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
maxl=1
minr=n
for i in range(m):
  l,r=map(int,input().split())
  maxl=max(maxl,l)
  minr=min(minr,r)

print(minr-maxl+1)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
maxl=1
minr=n
for i in range(m):
  l,r=map(int,input().split())
  maxl=max(maxl,l)
  minr=min(minr,r)

print(maxl-minr+1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
lr = [list(map(int, input().split())) for i in range(m)]
print(lr[0][1] - (lr[-1][0]-1))

Submission status: Wrong Answer

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

current_L = -10000
current_R = N+1000
for L,R in LRs:
    if current_L < L:
        current_L = L
    if current_R > R:
        current_R = R


if current_R < current_L:
    print(0)
print(current_R-current_L+1)