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: Accepted

Solution code:
n,m = map(int,input().split())
lr = [[0]*2]*m
for i in range(m):
  lr[i] = list(map(int,input().split()))
maxl = 0
minr = 10**5
for i in range(m):
  maxl = max(maxl,lr[i][0])
  minr = min(minr,lr[i][1])
if maxl>minr:
  print(0)
else:
  print(minr-maxl+1)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)

n,m=map(int,input().split())
d=[]
for _ in range(m):
    d.append(list(map(int,input().split())))
def ID(i):
    if i==1:
        return (d[0][0],d[0][1])
    l,r=ID(i-1)
    L=max(l,d[i-1][0])
    R=min(r,d[i-1][1])
    return (L,R)
s,g=ID(m)
print(max(g-s+1,0))

Submission status: Accepted

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

Submission status: Accepted

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)

A = 0
B = 100001

for a, b in zip(L, R):
    A = max(A, a)
    B = min(B, b)


if B-A < 0:
    print(0)
else:
    print(B-A+1)

Submission status: Accepted

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(max(0, min(r) - max(l) + 1))

Submission status: Accepted

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

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

count = 0
minimum = 0
maxmum = 10**9
for i in range(m):
    minimum = max(minimum, lr[i][0])
    maxmum = min(maxmum, lr[i][1])

if(maxmum >= minimum):
    print(maxmum - minimum + 1)
else:
    print(0)

Submission status: Accepted

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 if min(R)-max(L)>=0 else 0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=-10000000
r=10000000

for i in range(m):
  c,d=map(int,input().split())
  if l<c:
    l=c
  if d<r:
    r=d
    
print(max(r-l+1,0))

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
L,R = map(int,input().split())
pass_card = R - L + 1
for _ in range(M-1):
  L_now,R_now = map(int,input().split())
  if R_now < L or L_now > R:
    pass_card = 0
    break
  else:
    if R_now < R and L_now > L:
      R = R_now
      L = L_now 
    elif R_now < R and L_now < L:
      R = R_now
    elif R_now > R and L_now > L:
      L = L_now
    pass_card = R - L + 1
print(pass_card)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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) + 1 >= 0:
    print(min(r) - max(l) + 1)
else:
    print(0)

Submission status: Accepted

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

L = list(range(M))
R = list(range(M))

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

L_max = max(L)
R_min = min(R)

print(R_min - L_max + 1 if L_max <= R_min else 0)

Submission status: Accepted

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

def main():
    N, M = map(int, input().split())
    L =[0 for i in range(M)]
    R = [0 for i in range(M)]

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


main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

N,M = map(int,input().split())
L = [0]*M
R = [0]*M
#Li,Ri = map(int,input().split())

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

validID = min(R)-max(L)+1

if (validID>0):
    print(validID)
else:
    print("0")

Submission status: Accepted

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

N, M = map(int, input().split())
l, r = 1, N

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

print(max(0, r-l+1))

Submission status: Accepted

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())

lmax=max(l)
rmin=min(r)

print(max(rmin-lmax+1,0))

Submission status: Accepted

Solution code:
import numpy as np

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

L = 1
R = n

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
L = []
R = []
for i in range(m):
  t1, t2 = map(int, input().split())
  L.append(t1)
  R.append(t2)

maxl = max(L)
minr = min(R)
ans = minr - maxl + 1

if ans <= 0:
  print(0)
else:
  print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
max_L = 0
min_R = 10**5
for _ in range(M):
  L, R = map(int, input().split())
  max_L = max(max_L, L)
  min_R = min(min_R, R)
print(len(list(range(max_L, min_R+1))))

Submission status: Accepted

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

L.sort()
R.sort()

n =  R[0] -L[m-1] +1
if n > 0:
    print(n)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l = []
r = []
for _ in range(m):
    x, y = map(int, input().split())
    l.append(x)
    r.append(y)

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
sp = 0
ep = 10**5+1
for i in range(M):
    L, R = map(int, input().split())
    sp = max(sp, L)
    ep = min(ep, R)

print(max(0, ep-sp+1))

Submission status: Accepted

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

ListL = []
ListR = []

for i in range(M):
    ListL.append(LR[i][0])

for i in range(M):
    ListR.append(LR[i][1])

if (min(ListR)-max(ListL)+1)<0:
    print(0)

else:
    print(min(ListR)-max(ListL)+1)

Submission status: Accepted

Solution code:

# 左右の範囲を絞っていくことで共通要素のみを炙り出す
n,m = map(int,input().split())
l = -1
r = n+1
for i in range(m):
    l_i,r_i = map(int,input().split())
    l = max(l,l_i)
    r = min(r,r_i)
print(max(0,r-l+1))

Submission status: Accepted

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)
res=min(R)-max(L)+1
if res<0:
	print("0")
else:
	print(res)

Submission status: Accepted

Solution code:
import sys
from math import ceil, floor, sqrt, sin, cos, pi
from itertools import accumulate, permutations, combinations
from fractions import gcd # 最大公約数
from collections import deque, Counter
from operator import itemgetter
from heapq import heappop,heappush
sys.setrecursionlimit(10**7)
def lcm(x, y): return ((x * y) // gcd(x, y)) # 最小公倍数
# list(map(int, input().split()))
n, m = map(int, input().split())
minv = 1
maxv = n
for _ in range(m):
    l, r = map(int, input().split())
    if maxv > r: maxv = r
    if minv < l: minv = l
print(max(maxv - minv + 1, 0))

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n,m=map(int, input().split())
    l=[list(map(int, input().split())) for i in range(m)]
    a=10**5
    b=0
    for i in range(m):
        a=min(a,l[i][1])
    for i in range(m):
        b=max(b,l[i][0])
    print(max(0,a-b+1))
resolve()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
cards = [list(map(int, input().split())) for _ in range(M)]
left = cards[0][0]
right = cards[0][1]

for i in range(1, M):
    if left < cards[i][0]:
        left = cards[i][0]
    if right > cards[i][1]:
        right = cards[i][1]

if left > right:
    print(0)
else:
    ans = right - left + 1
    print(ans)

Submission status: Accepted

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

a=gate[0][0]
b=gate[0][1]
for i in range(1,M):
    a=max(a,gate[i][0])
    b=min(b,gate[i][1])

if b>=a:
    print(b-a+1)

else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
L = [0] * M
R = [0] * M
min = 1
max = N
X = 0
for i in range(M):
    L[i], R[i] = map(int, input().split())
    if L[i] > min:
        min = L[i]
    if R[i] < max:
        max = R[i]
    if R[i] < min or L[i] > max:
        X +=1


if X == 0:
    print(max-min+1)
else:
    print(0)

Submission status: Accepted

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

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

print(max(R-L+1, 0))

Submission status: Accepted

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

ans = [a[0],b[0]]

for i in range(m-1):
    if ans[0] < a[i+1]:
        ans[0] = a[i+1]
    if ans[1] > b[i+1]:
        ans[1] = b[i+1]

if (ans[1]-ans[0] +1)<= 0:
    print(0)
    exit()

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

Submission status: Accepted

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

l = 0
r = 10**10

for l1,r1 in lr:
    l = max(l, l1)
    r = min(r, r1)

print(max(0, r - l + 1))

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
li=[list(map(int,input().split())) for i in range(m)]
max_l,min_r=0,10**9

for i in li:
    max_l = max(max_l,i[0])
    min_r = min(min_r,i[1])
if min_r >= max_l:
    print(min_r-max_l+1)
else:
  print(0)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
L = 0
R = N
for i in range(M):
    l, r = map(int,input().split())
    L = max(L, l)
    R = min(R, r)

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
gate_list = [ list(map(int,input().split())) for i in range(m)]
#print(gate_list)
ans1 = gate_list[0][0]
ans2 = gate_list[0][1]
for i in range(1,m):
    ans1 = max(ans1,gate_list[i][0])
for i in range(1,m):
    ans2 = min(ans2,gate_list[i][1])
ans = ans2 - ans1
if ans < 0:
    print(0)
else:
    print(ans+1)

Submission status: Accepted

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(min(R)-max(L)+1)
else:
    print(0)

Submission status: Accepted

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

L_list = []
R_list = []

for i in range(M):
    L, R = list(map(int, input().split()))
    L_list.append(L)
    R_list.append(R)
    
ans = max(min(R_list) - max(L_list) + 1, 0)

print(ans)

Submission status: Accepted

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

n,m=map(int,input().split())
l={}
r={}
for i in range(m):
    l[i],r[i]=map(int,input().split())
min_=max(l.values())
max_=min(r.values())
ans=max_-min_+1
if ans>0:
    print(max_-min_+1)
else:
    print(0)

Submission status: Accepted

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

for i in range(M):
    Ltmp, Rtmp = map(int, input().split())
    L, R = max(L, Ltmp), min(R, Rtmp)

print(max(0, R - L + 1))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
L = 1
R = N
for i in range(M):
  Li, Ri = map(int, input().split())
  if Li > L:
    L = Li
  if Ri < R:
    R = Ri
if L <= R:
  print(R - L + 1)
else:
  print(0)

Submission status: Accepted

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

for i in range(M):
    Ltmp, Rtmp = map(int,input().split())
    L, R = max(L, Ltmp), min(R, Rtmp)

print(max(0,R-L+1))

Submission status: Accepted

Solution code:
def two_input():
    return map(int, input().split())

N, M = two_input()
L, R = [], []
for i in range(M):
    l, r = two_input()
    L.append(l)
    R.append(r)
    
maxl = max(L)
minr = min(R)

ans = 0
for i in range(N):
    if maxl <= i+1 <= minr:
        ans += 1

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
l = 1
r = N
for i in range(M):
  tmp_l ,tmp_r = map(int, input().split())
  l = max(l, tmp_l)
  r = min(r, tmp_r)
if r>=l:
  print(r-l+1)
else:
  print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding:utf-8
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(max(0, min(R) - max(L) + 1))

Submission status: Accepted

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

n,k=map(int,input().split())
L=[0]*(n+1)
und=0
top=10**5
for i in range(k):
    a,b=map(int,input().split())
    und=max(und,a)
    top=min(b,top)
print(top-und+1 if top>=und else 0)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
ll = []
rl = []
 
for i in range(m):
  l,r = list(map(int,input().split()))
  ll.append(l)
  rl.append(r)
  
if min(rl)<max(ll):
  print("0")
 
else:
  print(abs(min(rl)-max(ll)+1))

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
LR = sorted([list(map(int,input().split())) for i in range(m)])
c_min = 0
c_max = n+1

for i in range(m):
    l = LR[i][0]
    r = LR[i][1]
    
    if c_max < l:
        print(0)
        exit()
    else:
        c_min = max(c_min,l)
        c_max = min(c_max,r)
        
print(c_max - c_min + 1)

Submission status: Accepted

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

lmax = max(l)
rmin = min(r)
ans = rmin - lmax + 1
if ans < 0:
    ans = 0
print(ans)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
	N, M = map(int, input().split())
	lmax = 0
	rmin = N
	for _ in range(M):
		L,R = map(int, input().split())
		if L > lmax:
			lmax = L
		if R < rmin:
			rmin = R
	ans = rmin - lmax + 1
	if ans < 0:
		ans = 0
 
	print(ans)

Submission status: Accepted

Solution code:
n, m = list(map(int, input().split()))
#b = list(map(int, input().split()))
l = []
r = []
for i in range(m):
    x, y = list(map(int, input().split()))
    l.append(x)
    r.append(y)
L = l[0]
R = r[0]
# print("--------")
for i in range(1, m):
    if not((R < l[i] and R < r[i]) or (r[i] < L and l[i] < L)):
        L = max(L, l[i])
        R = min(R, r[i])
    else:
        print(0)
        exit()
    #print(L, R)

print(R-L+1)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
	N, M = map(int, input().split())
	lmax = 0
	rmin = N
	for _ in range(M):
		L,R = map(int, input().split())
		if L > lmax:
			lmax = L
		if R < rmin:
			rmin = R
	ans = rmin - lmax + 1
	if ans < 0:
		ans = 0

	print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n,m = map(int,input().split())
lr = []
for i in range(m):
    lr.append(list(map(int,input().split())))
card = [0] * (n+1)
for i in range(m):
    card[lr[i][0]-1] += 1
    card[lr[i][1]] -= 1
cum = np.cumsum(np.array(card))
ans = 0
for i in range(n+1):
    if cum[i] == m:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def solve():
    n, m = MI()

    maxl, minr = MI()
    for _ in range(m-1):
        l, r = MI()
        if maxl < l:
            maxl = l
        if r < minr:
            minr = r
    if maxl <= minr:
        print(minr - maxl + 1)
    else:
        print(0)

if __name__ == '__main__':
    solve()

Submission status: Accepted

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

left=0
right=10**8

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: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
minimum = []
maximum = []
for i in range(M):
  L, R = map(int, input().split())
  minimum.append(L)
  maximum.append(R)
  
ans = int(min(maximum) - max(minimum) + 1)
if ans < 0:
  print(0)
else:
  print(ans)

Submission status: Accepted

Solution code:
import sys


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

max_l = 0
min_r = sys.maxsize

for _ in range(M):
  l, r = map(int, input().split())
  max_l = max(max_l, l)
  min_r = min(min_r, r)
  
print(sum(
  max_l <= i <= min_r
  for i in range(1, N + 1)
))

Submission status: Accepted

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

l_list = [0] * m
r_list = [0] * m
for i in range(m):
    l_list[i], r_list[i] = [int(x) for x in input().split()]

print(max(min(r_list) - max(l_list) + 1, 0))

Submission status: Accepted

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

import sys
# import math
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product                # product(iter, repeat=n)
# from itertools import accumulate             # accumulate(iter[, f])
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from copy import deepcopy                    # to copy multi-dimentional matrix without reference
# from fractions import gcd                    # for Python 3.4


def main():
    mod = 1000000007                  # 10^9+7
    inf = float('inf')
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():  return int(input())
    def mi():  return map(int, input().split())
    def mi_0(): return map(lambda x: int(x)-1, input().split())
    def lmi(): return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():  return list(input())
    
    
    n, m = mi()
    l_list = []
    r_list = []
    for _ in range(m):
        l, r = mi()
        l_list.append(l)
        r_list.append(r)
    print(max(min(r_list)-max(l_list)+1, 0))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

left = 1
right = N

for i in range(M):
    L, R = map(int, input().split())
    left = max(left, L)
    right = min(right, R)

print(max(right - left + 1, 0))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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)
Lmax = max(L)
Rmax = min(R)
ans = Rmax-Lmax+1
if ans < 0:
  ans = 0
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
li = [list(map(int, input().split())) for _ in range(m)] #n行に１つずつ入力される整数
l_li = []
r_li = []
for l, r in li:
    l_li.append(l)
    r_li.append(r)
print(max(min(r_li) - max(l_li) +1, 0))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
import itertools


def sr(): return sys.stdin.readline().rstrip()


def ir(): return int(sr())


def lr(): return list(map(int, sr().split()))


N, M = lr()
l_list = []
r_list = []
for i in range(M):
    l, r = lr()
    l_list.append(l)
    r_list.append(r)

for i in range(M):
    if i == 0:
        maxL = l_list[i]
        minR = r_list[i]
        continue
    maxL = max(maxL, l_list[i])
    minR = min(minR, r_list[i])
ans = max(minR - maxL + 1, 0)
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
import itertools


def sr(): return sys.stdin.readline().rstrip()


def ir(): return int(sr())


def lr(): return list(map(int, sr().split()))


N, M = lr()
l_list = []
r_list = []
for i in range(M):
    l, r = lr()
    l_list.append(l)
    r_list.append(r)

# ans = set()
# left = l_list[M - 1]
# right = r_list[0]
tl_list = []
tr_list = []
for i in range(M):
    tmp_left = l_list[i]
    tmp_right = r_list[i]
    tl_list.append(tmp_left)
    tr_list.append(tmp_right)
left = max(tl_list)
right = min(tr_list)
# cards = sorted(l_list[i:] + r_list[:i + 1])
# cards = set(range(cards[0], cards[-1] + 1, 1))
# if (len(ans) == 0):
#     ans = cards
#     continue

# ans = cards & ans

ans = right - left + 1
if ans <= 0:
    print(0)
else:
    print(ans)

Submission status: Accepted

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

l,r=m[0][0],m[0][1]

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

print(max(r-l+1,0))

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
Card = [list(map(int,input().split())) for _ in range(m)]
l,r = 0,10**5+1
for c in Card:
    l = max(l,c[0])
    r = min(r,c[1])
print(max(r-l+1,0))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
maxL = 0
minR = float('inf')
for i in range(M):
    L, R = map(int, input().split())
    maxL = max(maxL, L)
    minR = min(minR, R)
if minR - maxL >= 0:
    print(minR - maxL + 1)
else:
    print(0)

Submission status: Accepted

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)

max_l = max(L)
min_r = min(R)
print(max(min_r - max_l + 1, 0))

Submission status: Accepted

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

Submission status: Accepted

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

lm,rm = 0,N
for i in range(0,M):
    l,r = map(int,input().split())
    lm = max(lm,l)
    rm = min(rm,r)
print(rm - lm + 1 if rm >= lm else 0)

Submission status: Accepted

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

#a = [0 for _ in range(n+1)]
ml = -1
mr = 10**6
for i in range(m):
  l,r = map(int,input().split())
  ml = max(ml,l)
  mr = min(mr,r)
if mr < ml :
  res = 0
else:
  res = mr-ml + 1

print(res)

Submission status: Accepted

Solution code:
import sys
N, M = map(int, sys.stdin.readline().split())
LR = sorted([list(map(int,sys.stdin.readline().split())) for i in range(M)])
L = 1
R = 10 ** 5
for i in LR:
    L = max(L, i[0])
    R = min(R, i[1])
if R - L >= 0:
    print(R - L + 1)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
LR=[]
for i in range(M):
  LR.append(list(map(int,input().split())))
l,r=LR[0][0],LR[0][1]
for i in range(M):
  if LR[i][1]<=r:
    r=LR[i][1]
  if l<=LR[i][0]:
    l=LR[i][0]
ans=r-l+1
if ans>0:
  print(ans)
else:
  print(0)

Submission status: Accepted

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

a = 1
b = 10**5

for _ in range(m):
    l,r = map(int, input().split())
    if l > a:
        a = l

    if r < b:
        b = r

if a <= b:
    print(b-a+1)

else:
    print(0)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l = list()
r = list()
for i in range(m):
    tmp_l, tmp_r = map(int, input().split())
    l.append(tmp_l)
    r.append(tmp_r)
maxl = max(l)
minr = min(r)
if maxl > minr:
    print(0)
elif maxl == minr:
    print(1)
else:
    print(minr - maxl + 1)

Submission status: Accepted

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

ml = 0
mm = 10**7
for v in L:
    ml = max(ml, v[0])
    mm = min(mm, v[1])

if (mm - ml + 1 <= 0):
    print(0)
    exit()
print(mm - ml + 1)

Submission status: Accepted

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


left = 0
right = float('inf')
for _ in range(m):
    l, r = map(int, input().split())
    left = max(left, l)
    right = min(right, r)

ans = 0
for i in range(1, n + 1):
    if left <= i <= right:
        ans += 1
print(ans)

Submission status: Accepted

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

gates = [0] * (n + 2)
for _ in range(m):
    l, r = map(int, input().split())
    gates[l] += 1
    gates[r + 1] -= 1

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

ans = 0
for i in range(n + 1):
    if gates[i] == m:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
# C - Prison

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

ansl = 1
ansr = n

for i in range(m):
    l, r = map(int, input().split())
    if l>ansl:
        ansl = l
    if r<ansr:
        ansr = r

print(max(ansr-ansl+1,0))

Submission status: Accepted

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

Submission status: Accepted

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

lmax = -1#初期値は反対（最大値は小さい、最小値は大きい）
rmin = n * 100
for i in range(m):
    l, r = map(int, input().split())#都度、入力
    lmax = max(l, lmax)#ソートは不要、最大・最小が分かれば良い
    rmin = min(r, rmin)

print(max(rmin - lmax + 1, 0))#マイナス値は取らない、植木算

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
L_MAX = 0
R_MIN = N
for i in range(M):
  L, R = map(int, input().split())
  if L > L_MAX:
    L_MAX = L
  if R < R_MIN:
    R_MIN = R

Ans=max(R_MIN - L_MAX + 1, 0)
print(Ans)

Submission status: Accepted

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

if max(l_lst) <= min(r_lst):
    print(min(r_lst) - max(l_lst) + 1)
else:
    print(0)

Submission status: Accepted

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