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

Solution (Python):
n,m=map(int,input().split())
a=[]
b=[]
c=0
d=0
for i in range(m):
    c,d=map(int,input().split())
    a.append(c)
    b.append(d)
c=max(a)
d=min(b)
if(c>d):
    print('0')
elif(d-c+1<=n):
    print('{0}'.format(d-c+1))
else:
    print('{0}'.format(n))

Solution (Python):
import sys
s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2sss = lambda n: [list(sys.stdin.readline().rstrip()) for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

import numpy as np  

def main():
    N, M = i2nn()
    LR = np.array(ii2nnn(M))
    L = LR[:,0]
    R = LR[:,1]
    n = np.min(R) - np.max(L) + 1
    n = max(n, 0)
    print(n)

main()

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

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

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

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

    L.append(tmp[0])
    R.append(tmp[1])


kukan = [max(L), min(R)]
sa = kukan[1] - kukan[0] +1
if sa > 0:
    print(sa)
else:
    print("0")

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

max_L = max([ i[0] for i in gate])
min_R = min([ i[1] for i in gate])
ans = 0
if max_L > min_R:
	ans = 0
else:
	ans = min_R - max_L + 1
print(ans)

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

Solution (Python):
import sys
from collections import deque

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(num_card, num_gate, inputs):
    gate_card = list(map(lambda x: string_to_int(x), inputs))
    min_card = 1
    max_card = num_card
    for d in gate_card:
        if d[0] > min_card:
            min_card = d[0]
        if d[1] < max_card:
            max_card = d[1]

    if max_card - min_card >= 0:
        return max_card - min_card + 1
    return 0


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    [N, M] = string_to_int(input())
    ret = solve(N, M, inputs(M))
    print(ret)

Solution (Python):
n,m=map(int,input().split())
li=[]
for i in range(m):
    a=list(map(int,input().split()))
    li.append(a)
l=0
r=n
for i in range(m):
    if li[i][0]>l:
        l=li[i][0]
    if li[i][1]<r:
        r=li[i][1]
print(max(0,r-l+1))

Solution (Python):
def smaller(x, y):
	if x < y:
		return x
	return y
def larger(x, y):
	if x > y:
		return x
	return y

N, M = map(int, input().split())
left = 0
right = N+1
for i in range(0, M):
	L, R = map(int, input().split())
	left = larger(left, L)
	right = smaller(right, R)
if right < left:
	print(0)
else:
	print(right-left+1)

Solution (Python):
n, m = [int(x) for x in input().split()]
min_num = 0
max_num = n

for x in range(m):
    tmp_min, tmp_max = map(int, input().split())
    min_num = max(min_num, tmp_min)
    max_num = min(max_num, tmp_max)

if max_num - min_num + 1 > 0:
    print(max_num - min_num + 1)
else:
    print(0)

Solution (Python):



N, M = map(int,input().split())
li = [list(map(int,input().split())) for i in range(M)]
L = [li[j][0] for j in range(M)]
R = [li[k][1] for k in range(M)]
ctr = 0

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

Solution (Python):
import numpy as np
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 right >= left:
    print(right-left+1)
else:
    print(0)

Solution (Python):
N, M = map(int, input().split())
 
l, r = 1, N
 
for _ in range(M):
    a, b = map(int, input().split())
    l = max(l, a)
    r = min(r, b)
 
print(max(0, r - l + 1))

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

l, r = 1, N

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

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

Solution (Python):
n, m = map(int, input().split())
left = 0
right = n
ls = list(range(n))

for i in range(m):
    l, r = map(int, input().split())
    if left < l-1:
        left = l-1
    if right > r:
        right = r
print(len(ls[left:right]))

Solution (Python):
n,m = map(int,input().split())
le = 0
ri = n
for i in range(m):
  a,b = map(int,input().split())
  le = max(le,a)
  ri = min(ri,b)
print(max(0,ri-le+1))

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

lower = 1
upper = N
for i in range(M):
    L, R = map(int, input().split())
    lower = max(L, lower)
    upper = min(R, upper)

ans = upper - lower + 1
ans = max(0, ans)
print(ans)

Solution (Python):
tamanho=list(map(int,input().rstrip().split()))
intervalo=[1,tamanho[0]]

for i in range(tamanho[1]):
  iterando=list(map(int,input().rstrip().split()))
  intervalo[0]=max(intervalo[0],iterando[0])
  intervalo[1]=min(intervalo[1],iterando[1])

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

Solution (Python):
n,m = map(int, input().split())
max_L = 0
min_R = 1000000

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) if (min_R-max_L) >= 0 else 0)

Solution (Python):
N, M = map(int, input().split())
Ls = []
Rs = []
for i in range(M):
  L, R = map(int, input().split())
  Ls.append(L)
  Rs.append(R)
Lmax = max(Ls)
Rmin = min(Rs)
r = 0 if Rmin<Lmax else Rmin-Lmax+1
print(r)

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)


N, M = LI()
LR = []

for i in range(M):
    LR.append(LI())

left, right = 0, inf
for lr in LR:
    left = max(left, lr[0])
    right = min(right, lr[1])

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

Solution (Python):



N, M = map(int,input().split())
li = [list(map(int,input().split())) for i in range(M)]
L = [li[j][0] for j in range(M)]
R = [li[k][1] for k in range(M)]
ctr = 0

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

Solution (Python):
n, m = map(int, input().split())
lmax = 0
rmin = n
for _ in range(m):
    l, r = map(int, input().split())
    lmax = max(lmax, l)
    rmin = min(rmin, r)

if lmax > rmin:
    print(0)
else:
    print(rmin - lmax + 1)

Solution (Python):
n, m = map(int, input().split())
left = 0
right = n
ls = list(range(1,n+1))

for i in range(m):
    l,r = map(int, input().split())
    if l - 1 > left:
        left = l - 1
    if r < right:
        right = r
print(len(ls[left:right]))

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n,k=LI()
  
  l=[0]*(n+1)
  for _ in range(k):
    a,b=LI()
    l[a-1]+=1
    l[b]-=1

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

  ans=0
  for x in l[:-1]:
    if x==k:
      ans+=1

  return ans

print(main())

Solution (Python):


import sys
input = sys.stdin.readline

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

ans = max(min(R) - max(L) + 1, 0)

print(ans)

Solution (Python):


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

l = max(L)
r = min(R)

ans = max(r - l + 1, 0)

print(ans)

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

lmax = 0
rmin = N

for i in range(M):
    l,r = map(int,input().split())
    lmax = max(lmax,l)
    rmin = min(rmin,r)

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

Solution (Python):
N, M = map(int, input().split())
G = [list(map(int, input().split())) for i in range(M)]
L = 1
R = N
for i in range(M):
  if G[i][0] > L:
    L = G[i][0]
  if G[i][1] < R:
    R = G[i][1]
  if L > R:
    print(0)
    break
if L <= R:
  print(R-L+1)

Solution (Python):
N,M = map(int,input().split())
L = []
R = []
LR = [list(map(int,input().split())) for _ in range(M)]
for i in range(M):
    L.append(LR[i][0])
countL = 0
countL = max(L)
for j in range(M):
    R.append(LR[j][1])
countR = 0
countR = min(R)
if countR - countL + 1 <= 0:
    print(0)
else:
    print(countR-countL+1)

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

Solution (Python):
n, m = map(int, input().split())
ls = []
rs = []

for _ in range(m):
    l, r = map(int, input().split())
    ls.append(l)
    rs.append(r)

l_max = max(ls)
r_min = min(rs)
master_card_num = r_min - l_max

if master_card_num < 0:
    print(0)
else:
    print(master_card_num + 1)

Solution (Python):
n, m = map(int, input().split())
left = -1
right = n+1

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

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

Solution (Python):
N,M=map(int,input().split())
x=[]
listA=[]
for i in range(0,M):
  x=list(map(int,input().split()))
  listA.append(x)
listB=[]
listC=[]
for i in range(0,M):
  listB.append(listA[i][0])
  listC.append(listA[i][1])
x=0
if max(listB)>min(listC):
  print(0)
else:
  print(abs(max(listB)-min(listC))+1)

Solution (Python):
n,m=map(int,input().split())
imos=[0]*(10**5+10)
maxr=-1
for _ in range(m):
    l,r=map(int,input().split())
    imos[l]+=1
    imos[r+1]-=1
    maxr=max(maxr,r)

for i in range(maxr+1):
    imos[i+1]+=imos[i]

ans=0

for i in range(maxr+1):
    if imos[i]==m:
        ans+=1

print(ans)

Solution (Python):
N, M = map(int, input().split())
R = []
L = []
for _ in range(M):
  temp = list(map(int, input().split()))
  L.append(temp[0])
  R.append(temp[1])
minR = min(R)
maxL = max(L)
if minR < maxL:
  print(0)
else:
  print(minR - maxL + 1)

Solution (Python):
n,m=map(int,input().split())
maxl=1
minr=n
while m:
  l,r=map(int,input().split())
  maxl=max(maxl,l)
  minr=min(minr,r)
  m-=1
print(max(0,minr-maxl+1))

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

for _ in range(M-1):
    l, r = map(int, input().split())
    if L < l:
        L = l

    if R > r:
        R = r

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

Solution (Python):
n,m = map(int,input().split())
lm = []
for _ in range(m):
    lm.append(tuple(map(int,input().split())))
l = lm[0][0]
r = lm[0][1]
for x in range(1,m):
    if lm[x][0] > l:
        l = lm[x][0]
    if lm[x][1] < r:
        r = lm[x][1]
    
if l > r:
    print(0)
else:
    print(r-l+1)

Solution (Python):
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())
a=L[0]
for i in range(M):
  if L[i]>a:
    a=L[i]
  else:
    a=a
b=R[0]
for i in range(M):
  if R[i]<b:
    b=R[i]
  else:
    b=b
if b-a<0:
  print("0")
else:
  print(b-a+1)

Solution (Python):
n,m=map(int,input().split())
x=1;y=n
for _ in range(m):
  l,r=map(int,input().split())
  if x<l:
    x=l
  if y>r:
    y=r
print(max(y-x+1,0))

Solution (Python):
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)

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

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

l_max = 0
r_min = 10**6
for i in range(m):
    l, r = map(int, input().split())
    l_max = max(l_max, l)
    r_min = min(r_min, r)
    
if l_max <= r_min:
    print(r_min - l_max + 1)
else:
    print(0)

Solution (Python):
n,m=map(int,input().split())
cnt=0
ln=[]
rn=[]
for i in range (m):
    l,r=map(int,input().split())
    ln.append(l)
    rn.append(r)
ln.sort(reverse=True)
rn.sort()
for i in range (n):
    if ln[0]<=i+1 and rn[0]>=i+1:
        cnt=cnt+1
print(cnt)

Solution (Python):
def prison(left, right):
    if left <= right:
        print(right-left+1)
    else:
        print(0)
        
N, M = map(int, input().split())
left_limit=0
right_limit=N
 
for i in range(M):
    LEFT, RIGHT = map(int, input().split())
    if LEFT > left_limit:
        left_limit = LEFT
    if RIGHT < right_limit:
        right_limit = RIGHT

prison(left_limit, right_limit)

Solution (Python):
n,m = map(int,input().split())
list = [0]*(n+2)
for i in range(m):
    l,r = map(int,input().split())
    list[l]+=1
    list[r+1]-=1
sum = 0
cnt = 0
for x in list:
    sum += x
    if sum == m :
        cnt+=1
print(cnt)

Solution (Python):
import sys
from sys import stdin
import numpy as np

N, M = map(int, input().split())
L, R = list(), list()
for i in range(M):
  l,r = stdin.readline().rstrip().split()
  L.append(int(l))
  R.append(int(r))

L = np.array(L)
R = np.array(R)

if R.min() - L.max() < 0:
  print(0)
else:
  print(R.min() - L.max() + 1)

Solution (Python):
N, M = map(int, input().split())
left=0
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(right-left+1)
else:
    print(0)

Solution (Python):
n, m = map(int, input().split())
l_lst = list()
for i in range(m):
    l_lst.append(input().split())

for i in range(0, len(l_lst)):
    for j in range(0, len(l_lst[i])):
        l_lst[i][j] = int(l_lst[i][j])

max_lst = list()
min_lst = list()
for i in range(0, len(l_lst)):
    max_lst.append(l_lst[i][0])
    min_lst.append(l_lst[i][1])

max_int = max(max_lst)
min_int = min(min_lst)

cnt = 0
for i in range(1, n+1):
    if max_int <= i and i <= min_int:
        cnt += 1

print(cnt)

Solution (Python):

N, M = map(int, input().split())
Table = [list(map(int, input().split())) for _ in range(M)]
Table.sort(key=lambda x:x[0])
Table.sort(key=lambda x:x[1])

first = Table[0][0]
end = Table[0][1]
Table.pop(0)
ans = 1

for task in Table:
    
    num = task[0]
    
    if num <= end:
        if num > first:
            first = num
    else:
        ans = 0
        break
    
if ans == 0:
    print(ans)
else:
    print(end-first+1)

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

min_value = 1
max_value = n

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

Answer = max(max_value - min_value +1,0)

print(Answer)

Solution (Python):
n, m = map(int, input().split())
l = 1
r = n
for _ in range(m):
    l0, r0 = map(int, input().split())
    l = max(l, l0)
    r = min(r, r0)
    if l > r:
        print(0)
        exit()
print(r - l + 1)

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

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

Solution (Python):
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())
cnt = 0
l_max = max(l[:m])
r_min = min(r[:m])
for i in range(n):
    if l_max <= i+1 <= r_min:
        cnt += 1
print(cnt)

Solution (Python):
n,m = map(int,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)
if(lmax>rmin):
    print(0)
else:
    print(rmin-lmax+1)

Solution (Python):
N, M = map(int, input().split())
left=0
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(right-left+1)
else:
    print(0)

Solution (Python):
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)

l = max(left)
r = min(right)

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

Solution (Python):
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())
ml = max(l)
mr = min(r)
if ml > mr:
    print(0)
else:
    print(mr - ml + 1)

Solution (Python):
data = list(map(lambda x: int(x),input().split(" ")))

cardNum = []

for n in range(data[1]):
	cardNum.append(list(map(lambda x: int(x),input().split(" "))))


card = [0]*(data[0])

check = [0,1000000]

for n in range(data[1]):
	if check[0] < cardNum[n][0]:
		check[0] = cardNum[n][0]
	if check[1] > cardNum[n][1]:
		check[1] = cardNum[n][1]

if check[1]-check[0] < 0:
	print(0)
else:
	print(check[1]-check[0]+1)

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

L,R = map(int,input().split(' '))
for i in range(M-1):
  inL,inR = map(int,input().split(' '))
  if inL > L:
    L = inL
  if inR < R:
    R = inR
    
print(max(0,R-L+1))

Solution (Python):
N, M = map(int, input().split())
ans = 0
L_list = []
R_list = []

for _ in range(M):
    l, r = map(int, input().split())
    L_list.append(l)
    R_list.append(r)

L = max(L_list)
R = min(R_list)

for i in range(1, N+1):
    if L <= i <= R:
        ans += 1

print(ans)

Solution (Python):
n, m = map(int, raw_input().split())
ml, mr = 0, n+1
for i in range(m):
    l, r = map(int, raw_input().split())
    ml = max(ml, l)
    mr = min(mr, r)
print max(mr-ml+1, 0)

Solution (Python):
n, m = list(map(int, input().split()))
r, l = 10 ** 5 + 1, 0
for i in range(m):
    li, ri = list(map(int, input().split()))
    if li > l:
        l = li
    if ri < r:
        r = ri

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

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

ll = []
rr = []
for i in range(m):
    l, r = [int(i) for i in input().split()]
    ll.append(l)
    rr.append(r)

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

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

max_L = -1
min_R = 10**5 + 1
for _ in range(M):
    L, R = map(int, input().split())
    max_L = max(max_L, L)
    min_R = min(min_R, R)

print(max(min_R - max_L + 1, 0))

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

max_L = -1
min_R = 10**5 + 1
for _ in range(M):
    L, R = map(int, input().split())
    max_L = max(max_L, L)
    min_R = min(min_R, R)

print(max(min_R - max_L + 1, 0))

Solution (Python):
n,m=map(int,input().split())
lr = [list(map(int,input().split())) for _ in range(m)]
min_id = 1
max_id = n
for v in lr:
  min_id = max(min_id,v[0])
  max_id = min(max_id,v[1])
if max_id < min_id:
  out = 0
else:
  out = max_id - min_id +1  
print(out)

Solution (Python):
n, m = map(int, input().split())
ls = [0]*m
rs = [0]*m

for i in range(m):
  l, r  = map(int, input().split())
  ls[i] = l
  rs[i] = r

print(max(min(rs)-max(ls)+1,0))

Solution (Python):
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)

ans = max(0, R - L + 1)

print(ans)

Solution (Python):
def kasanari(A, B, C, D):
    if B < C or A > D:
        return 0
    elif B <= D:
        if A >= C:
            return [A, B]
        else:
            return [C, B]
    else:
        if A >= C:
            return [A, D]
        else:
            return [C, D]

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

tmp = [L[0][0], L[0][1]]
L.pop(0)
for i in L:
    L1 = tmp[0]
    R1 = tmp[1]
    tmp = kasanari(L1, R1, i[0], i[1])
    if tmp == 0:
        print(0)
        exit()
print(tmp[1]-tmp[0]+1)

Solution (Python):
n,m = map(int,input().split())
card = [0]*n
l,r = 1,n
for i in range(m):
    tmp_l,tmp_r = map(int,input().split())
    if l < tmp_l:
        l=tmp_l
    if r > tmp_r:
        r = tmp_r
ans = 0
if r-l>=0:
    ans = r-l+1
print(ans)

Solution (Python):

n, m = map(int, input().split())
ids = [list(map(int, input().split())) for _ in range(m)]
l = []
r = []
for id in ids:
        l.append(id[0])
        r.append(id[1])
if max(l) <= min(r):
        print(min(r) - max(l) + 1)
else:
        print(0)

Solution (Python):
inf = input().split()
N = int(inf[0])
M = int(inf[1])
ls = []
m = 0
pair = [0,N]
for i in range(M):
  inf = input().split()
  L = int(inf[0])
  R = int(inf[1])
  pair[0] = max(pair[0], L)
  pair[1] = min(pair[1], R)
  if pair[0]>pair[1]:
    print(0)
    break
else:
  print(pair[1]-pair[0]+1)

Solution (Python):
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)
Rmin = min(R)
if Lmax>Rmin:
    print(0)
else:
    print(Rmin-Lmax+1)

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

for i in range(M-1):
    a,b= map(int, input().split())
    if a<L and R<b:
        N=N
    elif L<=a<=R:
        L=a
    elif L<=b<=R:
        R=b
    else:
        print(0)
        exit(0)
print(R-L+1)

Solution (Python):
N,M = map(int,input().split())
l=[]
r=[]
A=[]
for i in range(M):
        A.append(list(map(int,input().split())))

for i in range(M):
        l.append(A[i][0])
for i in range(M):
        r.append(A[i][1])

ans = min(r)-max(l)+1
if ans >= 0:
        print(ans)
else:
        print(0)

Solution (Python):
import numpy as np

a = input().split(" ")
N = int(a[0])
M = int(a[1])
gate = []
for i in range(M):
    a = input().split(" ")
    gate.append([int(a[0]),int(a[1])])

gate = np.array(gate)

max = gate.max(axis=0)[0]
min = gate.min(axis=0)[1]

if max > min:
    print(0)
elif max == min:
    if max <= N:
        print(1)
    else:
        print(0)
else:
    if N < max:
        print(0)
    elif max <= N and N <= min:
        print(N-max+1)
    else:
        print(min-max+1)

Solution (Python):
N,M=map(int,input().split())
L=[0]*M
R=[0]*M
max=N
min=1
for kai in range(M):
    L[kai],R[kai]=map(int,input().split())

for tan in range(M):
    if(L[tan]>min):
        min=L[tan]
    if(R[tan]<max):
        max=R[tan]
    if(L[tan]>max):
        kotae=0
        break
    if(R[tan]<min):
        kotae=0
        break
    kotae=max-min+1
print(kotae)

Solution (Python):
import sys
input = sys.stdin.readline

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

Solution (Python):
N,M=list(map(int,input().split()))
L=[0]*M
R=[0]*M
for i in range(M):
  L[i],R[i]=map(int,input().split())
a=max(L)
b=min(R)
if b>=a:
  print(b-a+1)
else:
  print(0)

Solution (Python):


def main():
    n, m = map(int, input().split())

    A = []
    B = []

    for _ in range(m):
        a, b = list(map(int, input().split()))
        A.append(a)
        B.append(b)

         
    l = max(A)
    r = min(B)
    

    if r >= l:
        return r - l + 1
    else:
        return 0

if __name__ == "__main__":
    print(main())



"""
for i in range(1, n + 1):
        for j in range(m):
            if gate[j][0] <= i and i <= gate[j][1]:
                if j == len(gate) - 1:
                    result += 1
                continue
            else:
                break
"""

Solution (Python):
n, m = map(int, input().split())
left = 0
right = n

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

Solution (Python):
n,m = input().split()
LR =  [list(map(int,input().split())) for i in range(int(m))]
s = max([i[0] for i in LR])
e = min([i[1] for i in LR])
print(max(e-s+1,0))

Solution (Python):
inf = input().split()
N = int(inf[0])
M = int(inf[1])
ls = []
m = 0
pair = [0,N]
for i in range(M):
  inf = input().split()
  L = int(inf[0])
  R = int(inf[1])
  pair[0] = max(pair[0], L)
  pair[1] = min(pair[1], R)
  if pair[0]>pair[1]:
    print(0)
    break
else:
  print(pair[1]-pair[0]+1)

Solution (Python):
inf = input().split()
N = int(inf[0])
M = int(inf[1])
ls = []
m = 0
pair = [0,N]
for i in range(M):
  inf = input().split()
  L = int(inf[0])
  R = int(inf[1])
  pair[0] = max(pair[0], L)
  pair[1] = min(pair[1], R)
  if pair[0]>pair[1]:
    print(0)
    break
else:
  print(pair[1]-pair[0]+1)

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

l = [0]*m
r = [0]*m
b = [0]*(n + 2)

count = 0

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

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

for i in range(1,n+1):
        if(i >= l_max and i <= r_min ):
                count += 1
        
print(count)

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

l = [0]*m
r = [0]*m
b = [0]*(n + 2)

count = 0

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

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

for i in range(1,n+1):
        if(i >= l_max and i <= r_min ):
                count += 1
        
print(count)

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

Solution (Python):
import sys
import math 
ni = lambda: int(ns())
na = lambda: list(map(int, input().split()))
ns = lambda: input()

N,M = na()
GATA = [0]*(N+1)
for i in range(M):
    L,R = na()
    GATA[L-1]+=1
    GATA[R]-=1
ans = 0
tt = 0
for i,t in enumerate(GATA):
    tt += t
    if tt == M:
        ans+=1
print(ans)

Solution (Python):
n,m=map(int,input().split())
ll=[];rl=[]

for i in range(m):
    l,r=map(int,input().split())
    ll.append(l,);rl.append(r)
    
tans=min(rl)-max(ll)+1
if tans>=0:print(tans)
else:print(0)

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

Solution (Python):
N,M = map(int, input().rstrip().split())
L_max = -10**10
R_min = 10**10
for i in range(M):
    L,R = map(int, input().split())
    if L_max < L:
        L_max = L
    if R < R_min:
        R_min = R
print(R_min-L_max+1 if L_max <= R_min else 0)

Solution (Python):
import sys
input = sys.stdin.readline

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

sidel = 1
sider = n
for lr in range_lr:
    sidel = max(sidel, lr[0])
    sider = min(sider, lr[1])
print(sider-sidel+1 if sider-sidel>=0 else 0)

Solution (Python):
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())

lt=max(L)
rt=min(R)

print(max(0,rt-lt+1))

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

left = 0
right = n

for i in li:
  left = max(i[0], left)
  right = min(i[1], right)
  
if right < left:
  print(0)
else:
  print(right - left + 1)

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

l = []
r = []

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

Solution (Python):
N, M = map(int, input().split())
LR = []
for _ in range(M):
    LR.append(list(map(int, input().split())))
mn, mx = 0, int(1e5)
for i, j in LR:
    if i >= mn:
        mn = i
    if j <= mx:
        mx = j
    
if mx < mn:
    print(0)
else:
    print(mx - mn + 1)

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

Solution (Python):
n, m = list(map(int, input().split()))
data = []
data1 = []
data2 = []
for i in range(m):
    data.append(list(map(int, input().split())))
for i in range(m):
    data1.append(data[i][0])
    data2.append(data[i][1])
if min(data2) - max(data1)  + 1 > 0:
    print(min(data2) - max(data1) + 1)
else:
    print(0)

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

l_max = 1
r_min = N
for m in range(M):
    left, right = map(int, input().split())
    l_max = max(l_max, left)
    r_min = min(r_min, right)

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

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

l_max = 1
r_min = N
for m in range(M):
    left, right = map(int, input().split())
    l_max = max(l_max, left)
    r_min = min(r_min, right)

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

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

Solution (Python):
n,m = map(int,input().split())
card = []
l = []
min_num = 0
max_num = 100000

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

for i in card:
	if i[0] > min_num:
		min_num = i[0]
	if i[1] < max_num:
		max_num = i[1]
	

if min_num > max_num:
	print(0)
else:
	print(max_num-min_num+1)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n,m=LI()

  l=[]
  for _ in ' '*m:
    a,b=LI()
    l.append([a,1])
    l.append([b,-1])

  l=sorted(l,key=lambda x:x[1],reverse=True)
  l=sorted(l,key=lambda x:x[0])

  sm=st=0
  for x in l:
    sm+=x[1]
    if sm==m:
      st=x[0]
    if sm!=m and st!=0:
      return x[0]-st+1

  return 0

print(main())

Solution (Python):
n, m = map(int, input().split())
i_max = n
i_min = 0
for x in range(m):
    l, r = map(int, input().split())
    i_max = min(r, i_max)
    i_min = max(l, i_min)

print(max(0,i_max - i_min + 1))