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=list(map(int,input().split()))
l=0
r=10**5+1
for _ in range(m):
  ll,rr=list(map(int,input().split()))
  l=max(l,ll)
  r=min(r,rr)
print(r-l+1 if r-l+1>=0 else 0)

Submission status: Accepted

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(max(0, last_index - start_index + 1))


resolve()

Submission status: Accepted

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())
p=1
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

    if right<l or r<left:
        p=0

if not p:
    print(p)
    exit()
print(right-left+1)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
L_list = []
R_list = []
count = 0
min_mn=float('inf')
max_mn=0
for i in range(M):
    L,R=map(int,input().split())
    min_mn = min(min_mn,R)
    max_mn = max(max_mn,L)

for i in range(1,N+1):
    if(i>=max_mn and i<=min_mn):
        count += 1
print(count)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
L_list = []
R_list = []
count = 0
min_mn=float('inf')
max_mn=0
for i in range(M):
    L,R=map(int,input().split())
    min_mn = min(min_mn,R)
    max_mn = max(max_mn,L)

for i in range(1,N+1):
    if(i>=max_mn and i<=min_mn):
        count += 1
print(count)

Submission status: Accepted

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

l = [L[0], R[0]] 
for i in range(1, M):
    if L[i] > l[0]:
        l[0] = L[i]
    if R[i] < l[1]:
        l[1] = R[i]

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

Submission status: Accepted

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(max(0,mx-mn+1))

Submission status: Accepted

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

Submission status: Accepted

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

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

for _ in range(m):
  a, b = map(int, input().split())
  l.append(a)
  r.append(b)
  
l_max = max(l)
r_min = min(r)

if l_max <= r_min:
  print(r_min - l_max + 1)
else:
  print(0)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
lr=[]
ma=1
mb=n

for i in range(m):
    a,b=map(int,input().split())
    ma=max(ma,a)
    mb=min(mb,b)
if ma>mb:
    print(0)
else:
    print(mb-ma+1)

Submission status: Accepted

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)

if r_min-l_max+1>=0:
    print(r_min-l_max+1)
else:
    print(0)

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
lr = [0, 10**6]
for _ in range(m):
  t = sorted(list(map(int, input().split())))
  lr[0] = max(lr[0], t[0])
  lr[1] = min(lr[1], t[1])

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

Submission status: Accepted

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

mi = 1
ma = N

for _ in range(M):
	a,b = map(int,input().split())
	mi = max(a,mi)
	ma = min(b,ma)

print(max(0,ma-mi+1))

#そのままでは計算量が多く通らないので、数学的な考察が必要。

Submission status: Accepted

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


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


    

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


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

Submission status: Accepted

Solution code:


N , M =(int(a) for a in input().split())
num = []

for i in  range(M) :
    L,R =(int(a) for a in input().split())
    num.append((L , R))

a = 0
b = 10 ** 5
for i in range(M) :
    a = max(a , num[i][0])
    b = min(b , num[i][1])

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

min_, max_ = 0, 10**5
for i in range(m):
    a, b = list(map(int, input().split()))

    min_ = max(min_, a)
    max_ = min(max_, b)

print(max(max_-min_+1, 0))

Submission status: Accepted

Solution code:
def main():
    n, m = map(int, input().split())
    left = 1
    right = n
    for _ in range(m):
        l, r = map(int, input().split())
        left = max(left, l)
        right = min(right, r)
        
    print(max(0, right - left + 1))
   
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

def main():
    N, M = map(int, input().split())
    Lmax = 0; Rmin = N+1
    for _ in range(M):
        L, R = map(int, input().split())
        Lmax = max(L, Lmax)
        Rmin = min(R, Rmin)
    ans = max(0, Rmin-Lmax+1)
    print(ans)
        
if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
maxl=0
minr=1000000
for i in range(M):
  L, R=map(int, input().split())
  maxl=max(maxl, L)
  minr=min(minr, R)
ans=minr-maxl+1
if ans<=0:
  ans=0
print(ans)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
Psum = [0] * (n+1)
for i in range(m):
    l,r=map(lambda x:int(x)-1,input().split())
    r += 1
    Psum[l] += 1
    Psum[r] -= 1
for i in range(n):
    Psum[i+1] += Psum[i]
cnt = 0
for i in range(n):
    if Psum[i] >= m:
        cnt += 1
print(cnt)

Submission status: Accepted

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

l=[]
r=[]
for i in range(M):
    l.append(L[i][0])
    r.append(L[i][1])
a=min(r)-max(l)+1
if a>=0:
    print(a)
else:
    print(0)

Submission status: Accepted

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
if ans<0:
	print(0)
else:
	print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
def LI(): return list(map(int,stdin.readline().rstrip().split()))

tmp = LI()
n,m = [tmp.pop(0) for _ in range(2)]

l = []
r = []

for i in range(m):
    tmp = LI()
    l.append(tmp.pop(0))
    r.append(tmp.pop(0))

ll = l[0]
rr = r[0]
for i in range(2):
    for j in range(m):
        if i == 0:
            if ll < l[j]:
                ll = l[j]
        else:
            if r[j] < rr:
                rr = r[j]

if rr<ll:
    print(0)
else:
    k = rr-ll+1
    if n<k:
        print(n)
    else:
        print(k)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
#import numpy as np
#import math
#from fractions import Fraction
#import itertools
from collections import deque
#import heapq
from fractions  import gcd

input=sys.stdin.readline
n,m=map(int,input().split())
lm=1
rm=n
for i in range(m):
    l,r=map(int,input().split())
    if l>lm:
        lm=l
    if r<rm:
        rm=r
ans=rm-lm+1 if rm>=lm else 0
print(ans)

Submission status: Accepted

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

left = 1
right = N+1
for i in range(M):
  left = max(left, datas[i][0])
  right = min(right, datas[i][1])

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

Submission status: Accepted

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

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())
    
L.sort()
R.sort()

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

cnt =0 

for i in range(l,r+1):
    cnt+=1

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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(max(0, card_r - card_l + 1))

Submission status: Accepted

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)

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

Submission status: Accepted

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)
    ans = mi-mx+1
    if ans<0: 
        print(0)
        return
    print(mi-mx+1)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from collections import deque, defaultdict
from itertools import combinations, combinations_with_replacement
import bisect
from math import factorial, sqrt, log, ceil, floor

def main():
    #N, M = map(int, input().split())
    N, M = map(int, input().split())

    max_L = None
    min_R = None
    for i in range(M):
        L, R = map(int, input().split())
        if max_L is None or max_L < L:
            max_L = L
        if min_R is None or min_R > R:
            min_R = R

    if max_L <= min_R:
        num = min_R - max_L + 1
    else:
        num = 0
    print(num)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main() -> None:
    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(max(0, right - left + 1))


if __name__ == '__main__':
    main()

Submission status: Accepted

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


def main():
    n, m = map(int, input().split())
    range_list = [list(map(int, input().split())) for _ in range(m)]
    min_list = [x[0] for x in range_list]
    max_list = [x[1] for x in range_list]
    min_id = max(min_list)
    max_id = min(max_list)
    answer = max_id - min_id + 1
    answer = max(answer, 0)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

arr = [0 for i in range(N+2)]

for i in range(M):
  l,r = map(int,input().split())
  arr[l] += 1
  arr[r+1] -= 1
  
for i in range(1,len(arr)):
  arr[i]+=arr[i-1]

cnt = 0

for i in range(len(arr)):
  if arr[i] >= M:
    cnt += 1
print(int(cnt))

Submission status: Accepted

Solution code:
n, m = list(map(int, input().split()))
low = 0
high= n+1

for i in range(m):
  temp = list(map(int, input().split()))
  if temp[0] > low:
    low = temp[0]
  if temp[1] < high:
    high = temp[1]


if high-low >= 0:
  print(high-low+1)
else:
  print(0)

Submission status: Accepted

Solution code:
n, m = list(map(int, input().split()))
low =[]
high=[]

for i in range(m):
  temp = list(map(int, input().split()))
  low.append(temp[0])
  high.append(temp[1])
  
maxlow= max(low)
minhigh=min(high) 

if minhigh-maxlow >= 0:
  print(minhigh-maxlow+1)
else:
  print(0)

Submission status: Accepted

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

for i in range(m-1):
    l_new,r_new = map(int,input().split())
    if l_new > l:
        l = l_new
    if r_new < r:
        r = r_new

if r - l < 0:
    print(0)
else:
    print(r-l+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a=list(range(1,n+1))
l=0
l1=0
r=0
r1=10**10

for i in range(m):
	l,r=map(int,input().split())
	if l>l1:
		l1=l
	if r<r1:
		r1=r
b=list(range(l1,r1+1))
a=list(set(b) & set(a))
print(len(list(a)))

Submission status: Accepted

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
if high>=low:
  print(high-low+1)
else:
  print(0)

Submission status: Accepted

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

Submission status: Accepted

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)
if min(lia)-max(li)+1 <= 0:
    print(0)
else:
    print(min(lia)-max(li)+1)

Submission status: Accepted

Solution code:
from itertools import accumulate

n, m = [int(x) for x in input().split()]
a = [0 for i in range(n + 2)]
for i in range(m):
    l, r = [int(x) for x in input().split()]
    a[l] += 1
    a[r + 1] -= 1
a = list(accumulate(a))
print(a.count(m))

Submission status: Accepted

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)


resolve()

Submission status: Accepted

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 if r - l >= 0 else 0)

Submission status: Accepted

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)
if rr-ll >= 0:
    print(rr-ll+1)
else:
    print(0)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
limL = -454554565454
limR = 7896541265478456
for i in range(M):
  L, R = map(int, input().split())
  limL = max([limL, L])#lとしては，大きい方が嬉しい
  limR = min([limR, R])#rは小さい方がいい

print(limR-limL+1) if limR-limL+1>0 else print(0)

Submission status: Accepted

Solution code:
n,m= map(int,input().split())
l1,r1 = map(int,input().split())
lis = [l1,r1]

for i in range(m-1):
  l,r = map(int,input().split())
  if lis[0]<=l<lis[1] and lis[0]<r<=lis[1]:
    lis = [l,r]
  elif (lis[1]<l and lis[1]<r) or (l<lis[0] and r<lis[0]):
    print(0)
    exit()
  elif lis[0]<l<=lis[1] and lis[1]<r:
    lis[0] = l
  elif l<lis[0] and lis[0]<=r<lis[1]:
    lis[1] = r
    
print(lis[1]-lis[0]+1)

Submission status: Accepted

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

L = 1
R = 10**5
ans = 1

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

if ans != 0:
    ans = R - L + 1

print(ans)

Submission status: Accepted

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)

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

Submission status: Accepted

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
if l_max <= r_min:
    print(r_min - l_max + 1)
else:
    print(0)

Submission status: Accepted

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(max(0,R-L+1))

Submission status: Accepted

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(max(Rmin - Lmax + 1, 0))

Submission status: Accepted

Solution code:
def solve():
    N, M = list(map(int, input().split()))
    s = [0] * (N+1)
    
    for _ in range(M):
        l, r = list(map(int, input().split())) 
        s[l] += 1
        if r+1 != N+1:
            s[r+1] -= 1
    
    cnt = 0
    ruiseki = [0]
    for i in range(1, N+1):
        ruiseki.append(ruiseki[i-1] + s[i])
        if ruiseki[i] == M:
            cnt += 1

    print(cnt)

if __name__ == "__main__":
    solve()

Submission status: Accepted

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) if limR-limL+1>0 else print(0)

Submission status: Accepted

Solution code:
# input & check
N, M = map(int, input().split())
x, y = 0, N
for i in range(1, M + 1):
    L, R = map(int, input().split())
    x = max(x, L)
    y = min(y, R)

print(len(range(x, y + 1)))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
L, R = 0, 100000
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())

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

print(len(list(range(l,r+1))))

Submission status: Accepted

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

Submission status: Accepted

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

Solution code:
#!/usr/bin/env python3
import sys
try:
    from typing import List
except ImportError:
    pass


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    ml = 1
    mr = N
    for Li, Ri in zip(L, R):
        ml = max(ml, Li)
        mr = min(mr, Ri)

    print(max(mr - ml + 1, 0))


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

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

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
if count >= 0:
    print(count)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

_min = 1
_max = N

for _ in range(M):
    l, r = map(int, input().split(' '))
    _min = max(_min, l)
    _max = min(_max, r)

print(max(0, _max - _min + 1))

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
p = []
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(max(0,ansr-ansl+1))

Submission status: Accepted

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

Submission status: Accepted

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

L,R= map(int,input().split())

for i in range(M-1):
    x,y= map(int,input().split())
    L = max(L,x)
    R = min(R,y)

print(R-L+1 if L<=R else 0)

Submission status: Accepted

Solution code:
n,m=[int(x) for x in input().rstrip().split()]
ll=[]
rl=[]
for i in range(m):
  l,r=[int(x) for x in input().rstrip().split()]
  ll.append(l)
  rl.append(r)

ans=0
if min(rl)-max(ll)<0:
  print(0)
else:
  print(min(rl)-max(ll)+1)

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)
tmp = min(R)-max(L)+1

if tmp >= 1:
    print(tmp)
else:
    print(0)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
#a=[[0]*2 for ii in range(m)]
for ii in range(m):
  #a[ii][:]=map(int,input().split())
  a,b=map(int,input().split())
  if ii==0:
    #r=[a[ii][0],a[ii][1]]
    r=[a,b]
  else:
    #r=[max(a[ii][0],r[0]),min(a[ii][1],r[1])]
    r=[max(a,r[0]),min(b,r[1])]
q=r[1]-r[0]+1
if q<0:
  print(0)
else:
  print(q)

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())
a = max(A)
b = min(B)
if a <= b:
  print(b+1-a)
else:
  print(0)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat May 16 19:11:36 2020

@author: Kanaru Sato
"""

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

currentl,currentr = list(map(int,input().split()))
flag = 1

for i in range(m-1):
    l,r = list(map(int,input().split()))
    if currentl <= l and l <= currentr:
        currentl = l
    if currentl <= r and r <= currentr:
        currentr = r
    if currentr < l or r < currentl:
        flag = 0
        print(0)
        break

if flag == 1:
    print(currentr-currentl+1)

Submission status: Accepted

Solution code:
from collections import Counter 
from collections import defaultdict
from collections import deque
import itertools
import heapq
import sys
sys.setrecursionlimit(10**6)

#n=int(input())
#n,m=list(map(int,input().split()))
#a=list(map(int,input().split()))
input_list = lambda : list(map(int,input().split()))


n,m=input_list()
l=0
r=n
for _ in range(m):
    now=input_list()
    l=max(l,now[0])
    r=min(r,now[1])


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)

def solve(l1, r1, l2, r2):
    if r1 < l2 or r2 < l1:
        return None
    elif l1 < l2 and r1 < r2:
        return [l2, r1]
    elif l1 >= l2 and r1 <= r2:
        return [l1, r1]
    elif l1 <= l2 and r2 <= r1:
        return [l2, r2]
    else:
        return [l1, r2]


def main():
    n, m = map(int, input().split())
    ans = [1, n]
    for _ in range(m):
        l, r = map(int, input().split())
        # print(l, r)
        ans = solve(ans[0], ans[1], l, r)
        # print(ans)
        if ans is None:
            print(0)
            exit(0)
    print(ans[1] - ans[0] + 1)

if __name__ == '__main__':
    main()

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)

L.sort()
R.sort()

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
l=list()
r=list()
for n in range(M):
  L,R=map(int,input().split())
  l.append(L)
  r.append(R)
if min(r)>=max(l):
  print(min(r)-max(l)+1)
else:
  print('0')