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

import re
import math
from collections import defaultdict
from collections import deque
import itertools
from copy import deepcopy
import random
import time
import os
import queue
import sys
import datetime
from functools import lru_cache

readline=sys.stdin.readline
sys.setrecursionlimit(2000000)

alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(N)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def is_power(a,b):
    now=b
    while now<a:
        now*=b
    if now==a:return True
    else:return False
def bin_(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def get_facs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:
        return num
def get_primes(n,type="int"):
    if n==0:
        if type=="int":return []
        else:return [False]
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def is_prime(num):
    if(num<=1):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(a,b,c):
    if a:return b
    else:return  c
def join(A,c=""):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s
def factorize(n,type_="dict"):
    b = 2
    list_ = []
    while b * b <= n:
        while n % b == 0:
            n //= b
            list_.append(b)
        b+=1
    if n > 1:list_.append(n)
    if type_=="dict":
        dic={}
        for a in list_:
            if a in dic:
                dic[a]+=1
            else:
                dic[a]=1
        return dic
    elif type_=="list":
        return list_
    else:
        return None
def floor_(n,x=1):
    return x*(n//x)
def ceil_(n,x=1):
    return x*((n+x-1)//x)
def hani(x,min_,max_):
    ret=x
    if x<min_:ret=min_
    if x>max_:ret=max_
    return ret
def seifu(x):
    return x//abs(x)

def main():
    N,M=list(map(int,list(input().split())))
    LR=[]
    for a in range(M):
        LR.append(list(map(int,input().split())))
    l=LR[0][0]
    r=LR[0][1]
    for a in range(M):
        l=max(l,LR[a][0])
        r=min(r,LR[a][1])
    d=max(r-l+1,0)
    print(d)


main()

Solution (Python):
n , m = map(int , input().split())
l = -10
r = 100000
for i in range(m):
  j = list(map(int , input().split()))
  l = max(l , j[0])
  r = min(r , j[1])
print(max(min(r - l + 1 , n) , 0))

Solution (Python):
import numpy as np
def main():
    N, M = map(int, input().split())
    maxl = -float('inf')
    minr = float('inf')
    for _ in range(M):
        l, r = map(int, input().split())
        if l >= maxl:
            maxl = l
        if r <= minr:
            minr = r
    val = minr - maxl + 1
    if val >= 0:
        print(val)
    else:
        print(0)
main()

Solution (Python):


import sys
input= lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**9)
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
def lispin(t=int):return list(pin(t))


def resolve():
    N,M=pin()
    x=0
    y=N
    for i in range(M):
        a,b=pin()
        x=max(x,a)
        y=min(y,b)
        
    t=(y-x+1)
    if t<0:t=0
    print(t)
            
        

resolve()

Solution (Python):
def resolve():
    n, m = map(int, input().split())
    lst = [list(map(int, input().split())) for _ in range(m)]

    prev = lst[0]
    for new in lst[1:]:
        prev = [max(prev[0], new[0]), min(prev[1], new[1])]

    print(len(range(prev[0], prev[1]+1)))


resolve()

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

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

Solution (Python):
N, M = map(int, input().split())
LRm = [list(map(int, input().split())) for _ in range(M)]
min_n = 0
max_n = N
for l, r in LRm:
    min_n = max(min_n, l)
    max_n = min(max_n, r)
print(max(max_n-min_n+1, 0))

Solution (Python):
N, M = list(map(int, input("").split(" ")))
permit = [list(map(int, input("").split(" "))) for i in range(M)]
l,r = 1, N
for i,j in permit:
  l = max(l, i)
  r = min(r, j)
print(max(r-l+1,0))

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

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

min_a = 1
max_b = N

for i in range(M):
    a,b = [int(i) for i in input().split()]
    if min_a < a:
        min_a = a
    if max_b > b:
        max_b = b

print(max(0, max_b-min_a+1))

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

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

for i in range(M):
    l, r = map(int, input().split())
    
    if i==0:
        left = l
        right = r

    else:
        if l>left:
            left = l
        if r<right:
            right = r

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

Solution (Python):
n,m=map(int,input().split())
l = 1
r = n
for j in range(m):

    l_i,r_i = map(int,input().split())

    l = max(l,l_i)
    r = min (r,r_i)

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

Solution (Python):
def prison(n, m, lrli):

    rmin = 10**5
    lmax = 0

    for i in lrli:
        rmin = min(rmin, i[1])
        lmax = max(lmax, i[0])

    return rmin-lmax+1 if rmin >= lmax else 0

def main():
    n, m = map(int, input().split())
    lrli = [list(map(int, input().split()))for i in range(m)]
    print(prison(n, m, lrli))

if __name__ == '__main__':
    main()

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

print(0 if rmin-lmax+1<=0 else rmin-lmax+1)

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

print(0 if rmin-lmax+1<=0 else rmin-lmax+1)

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

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce
from fractions import gcd
import sys
def I(): return int(input())
def Is(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(input())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = float('inf')


n, m = Is()
LR = TIR(m)

l = -INF
r = INF

for l_, r_ in LR:
    l = max(l, l_)
    r = min(r, r_)

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

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

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)
  
ans = min_r - max_l + 1
if ans < 0:
  ans = 0
  
print(ans)

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

Solution (Python):
N, M = map(int, input().split())
Llist = []
Rlist = []

for i in range(M):
    l, r = map(int, input().split())
    Llist.append(l)
    Rlist.append(r)

if min(Rlist)-max(Llist)+1 > 0:
    print(min(Rlist)-max(Llist)+1)
else:
    print(0)

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

l = 1
r = N

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

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

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

ll = list()
lr = list()
for i in range (M):
    l, r = map(int, input().split())
    ll.append(l)
    lr.append(r)

ans = min(lr) - max(ll) + 1

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

Solution (Python):


N, M = map(int, input().split())
left = 0
right = 10 ** 5
for _ in range(M):
    l, r = map(int, input().split())
    left, right = max(left, l), min(right, r)

print(len([i for i in range(left, right + 1)]) if left <= right else 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)
  
ans=min(R)-max(L)+1
if ans>=0:
  print(ans)
else:
  print(0)

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

lmax,rmin=0,10**5
for _ in range(m):
  l,r=map(int,input().split())
  lmax=max(lmax,l)
  rmin=min(rmin,r)

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

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

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

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

Solution (Python):
import numpy as np
_, M = map(int, input().split())
L, R = np.array([tuple(map(int, input().split())) for _ in range(M)], dtype=np.int32).T
print(max(0, R.min() - L.max() + 1))

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


l_max = 0
r_min = float('inf')

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

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)
big=min(r)
small=max(l)
print(big-small+1 if big-small>=0 else 0)

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

LList = []
RList = []

for i in range(M):
    L = List[i][0]
    R = List[i][1]
    
    LList.append(L)
    RList.append(R)
    

print(max((min(RList)-max(LList))+1,0))

Solution (Python):
n,m=map(int,input().split())
hidari=-10**100
migi=10**100
keep=1
for i in range(m):
  l,r=map(int,input().split())
  if l>migi or r<hidari:
    keep=0
    break
  elif hidari<=l<=r<=migi:
    hidari=l
    migi=r
  elif l<=hidari<=migi<=r:
    pass
  elif l<=hidari<=r<=migi:
    migi=r
  elif hidari<=l<=migi<=r:
    hidari=l

if keep==1:
  res=migi-hidari+1       
  print(min(res,n))
else:
  print(0)

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

L = [] 
R = []
for i in range(M):
  L.append(a[i][0])
  R.append(a[i][1])

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

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

l=max(L)
r=min(R)
ans=(r+1-l)

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

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

l=L[0]
r=R[0]

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

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

Solution (Python):

n, m = map(int, input().split())
l = []
r = []
for _ in range(m):
    tmp=list(map(int,input().split()))
    l.append(tmp[0])
    r.append(tmp[1])
lmax = max(l)
rmin = min(r)

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

Solution (Python):
import sys
n,m = map(int,input().split())
ls = [list(map(int,input().split())) for i in range(m)]
a = ls[0][0]
b = ls[0][1]
for j in range(1,m):
    a = max(a,ls[j][0])
    b = min(b,ls[j][1])
    if b-a<0:
        print(0)
        sys.exit()
print(b-a+1)

Solution (Python):
N,M = map(int,input().split())
a = []
b = []
for i in range(M):
    x,y = map(int,input().split())
    a.append(x)
    b.append(y)
if max(a) <= min(b):
    print(min(b)-max(a)+1)
else:
    print(0)

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

l = 0
r = (10**5)+1

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

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

Solution (Python):
def main():
    N, M = list(map(int, input().split(' ')))
    max_l, min_r = 1, N
    for _ in range(M):
        L, R = list(map(int, input().split(' ')))
        max_l = max([max_l, L])
        min_r = min([min_r, R])
    print(max([min_r - max_l + 1, 0]))


if __name__ == '__main__':
    main()

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

maxl = 0
minr = 10**5
for i in range(m):
  l, r = map(int, input().split())
  maxl = max(maxl, l)
  minr = min(minr, r)

print(max(minr-maxl+1, 0))

Solution (Python):
n, m = map(int, input().split())
max = float("INF")
min = 0
for i in range(m):
    l, r = map(int, input().split())
    if max > r:
        max = r
    if min < l:
        min = l
if max - min + 1 >= 0:
    print(max - min + 1)
else:
    print(0)

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

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

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

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)

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)

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

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

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

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

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

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

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

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

Solution (Python):


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

Solution (Python):



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


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

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

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

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

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

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

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

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

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

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

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

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

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

Solution (Python):


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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)

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

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

Solution (Python):


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)

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

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

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

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

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

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

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

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)

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

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

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

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

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

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

Solution (Python):
n, m = 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]

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

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

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

Solution (Python):


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

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