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

lans = 1
rans = n
for i in range(m):
  l,r = map(int,input().split())
  lans = max(lans,l)
  rans = min(rans,r)
if lans <= rans:
  print(len(list(range(lans,rans+1))))
else:
  print(0)

Solution (Python):
card, gate = map(int, input().split())
max_l = 0
min_r = 100000
for i in range(gate):
  l, r = map(int, input().split())
  max_l = max(max_l, l)
  min_r = min(min_r, r)
print(len(range(max_l, min_r+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)
max_l=max(L)
min_r=min(R)
if min_r-max_l>=0:
    print(min_r-max_l+1)
else:
    print(0)

Solution (Python):
import math
import fractions
import copy

def j(q):
    if q==1: print("YES")
    elif q == 0:print("NO")
    exit(0)
rem = pow(10,9)+7
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def printrow(a):
    for i in range(len(a)):
        print(a[i])
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)



m,n= (int(i) for i in input().split())       








left = 1
right = m
for p in range(n):
    l,r = (int(i) for i in input().split())
    left = max(l,left)
    right = min(r,right)
if left<=right:
    print(right-left+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)
L.sort()
R.sort()
A = R[0] - L[-1] +1
print(max(A,0))

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

Solution (Python):
N, M = input().split()
N = int(N)
M = int(M)
L_list =[]
R_list = []
for h in range(M):
    L, R = input().split()
    L_list.append(L)
    R_list.append(R)

L_list = map(int,L_list)
R_list = map(int,R_list)

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

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

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

Solution (Python):
N,M=map(int,input().split())
L=[0 for i in range(M)]
R=[0 for i in range(M)]
for i in range(M):
    L[i],R[i]=map(int,input().split())
print(max(min(R)-max(L)+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)

maxl = max(L)
minr = min(R)

ans = minr - maxl + 1
if ans > 0:
    print(ans)
else:
    print(0)

Solution (Python):
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)
count = 0
ma = max(l)
mi = min(r)
for i in range(1,n+1):
        if ma <= i <= mi: 
            count += 1
print(count)

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

ans=min(R)-max(L)+1
print("0" if ans<0 else ans)

Solution (Python):
N,M=map(int,input().split())
L=0
R=0
mx=0
mn=100000
for i in range(M):
  L, R = map(int,input().split())
  if L > mx:
      mx=L
  if R < mn:
      mn=R

result =mn-mx+1
if result < 0:
    result = 0
print(result)

Solution (Python):
n, m = [int(x) for x in 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)

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

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

Solution (Python):


import sys
import numpy as np

N, M = map(int, sys.stdin.readline().split(" ", 2))

L = 0
R = M + N
for line in sys.stdin:
    l, r = map(int, line.split(" ", 2))
    if l > L:
        L = l
    if r < R:
        R = r
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)

if max(L) <= min(R):
    print(min(R) - max(L) + 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-1)
	r.append(N-R)

ans = N-max(l)-max(r)

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

Solution (Python):
isstr=input()
n,m = [int(i) for i in isstr.split(' ')]
l=[0]*m
r=[0]*m
for i in range(m):
    l[i],r[i]=[int(i)-1 for i in input().split(' ')]
x=min(r)-max(l)+1
x=x if x>0 else 0
print(x)

Solution (Python):
def main():
    N, M = map(int, input().split())
    id_and_gate = []
    for m in range(0, M):
        tmp = [int(x) for x in input().split()]
        id_and_gate.append(tmp)

    l = 1
    r = 100000
    for j in range(0, M):
        if id_and_gate[j][0] >= l:
            l = id_and_gate[j][0]
        if id_and_gate[j][1] <= r:
            r = id_and_gate[j][1]

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

if __name__ == "__main__":
    main()

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

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

L_list.sort(reverse=True)
R_list.sort()
MAX = L_list[0]
MIN = R_list[0]
cnt=0
for i in range(1,N+1):
    if(i<=MIN and i>=MAX):
        cnt+=1
print(cnt)

Solution (Python):
def main():
    n, m = map(int, input().split())
    l = []
    r = []
    ans = 0
    for i in range(m):
        a, b = map(int, input().split())
        l.append(a)
        r.append(b)
    ml = max(l)
    mr = min(r)
    for i in range(1, n+1):
        if i >= ml and i <= mr:
            ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n,m,*t=map(int,open(0).read().split());a,c=[0]*-~n,0
for l,r in zip(t[::2],t[1::2]):a[l-1]+=1;a[r]-=1
for i in range(n):a[i+1]+=a[i];c+=a[i]==m
print(c)

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

li = [[0, 99999999999]]
L_max   = 0
R_min  = 999999999999
for i in range(M):
    li.append(list(map(int, input().split())))
    if (L_max  < li[i+1][0] ):
        L_max = li[i+1][0]
        if (R_min  > li[i+1][1]):
            R_min = li[i+1][1]
    elif (R_min > li[i+1][1]):
            R_min = li[i+1][1]

if (L_max  < li[M][0] ):
       L_max = li[M][0]
       if (R_min  > li[M][1]):
          R_min = li[M][1]
elif (R_min  > li[M][1]):
            R_min = li[M][1]

if (L_max > N) or (L_max > R_min):
    print("0")
else:
    print(R_min - L_max + 1)

Solution (Python):
N, M = map(int, input().split(" "))
L = 1
R = N
ans = 0
for m in range(M):
    L_in, R_in = map(int, input().split(" "))
    if L_in > L:
        L = L_in
    if R_in < R:
        R = R_in
if L > R:
    ans = 0
else:
    ans = R - L + 1
print(ans)

Solution (Python):
import math

def main():
    n, m = [int(x) for x in input().split(" ")]
    gates = []
    mi = n
    ma = 0

    for x in range(m):
        l, r = [int(x) for x in input().split(" ")]
        gates.append((l, r))

    mi = gates[0][1]
    ma = gates[0][0]

    for gate in gates:
        
        if ma <= gate[0]:
            ma = gate[0]

        if mi >= gate[1]:
            mi = gate[1]

    res = 0
    if mi < ma:
        res = 0
    else:
        res = mi - ma + 1

    print(res)

if __name__ == "__main__":
    main()

Solution (Python):

import sys

s = (input().split(' '))

n,m = [int(i) for i in s]

l_list = []
r_list = []

for i in range(m):
    s = (input().split(' '))
    l,r = [int(i) for i in s]
    l_list.append(l)
    r_list.append(r)

range = (max(l_list), min(r_list))

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

Solution (Python):
N, M = map(int, input().split())
L, R = zip(*[map(int, input().split()) for _ in range(M)])

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

print(ans)

Solution (Python):
N,M = map(int,input().split())
LR = [[int(x) for x in input().split()] for _ in range(M)]

L_all,R_all = 1,N
for L,R in LR:
    L_all = max(L_all,L)
    R_all = min(R_all,R)

print(max(0,R_all+1-L_all))

Solution (Python):


N,M = [int(x) for x in input().split()]
Llist =[]
Rlist =[]
for i in range (M):
    L,R = [int(x) for x in input().split()]
    Llist.append(L)
    Rlist.append(R)
 
lmax = max(Llist)
Rmax = min(Rlist)

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

Solution (Python):
n,m,*t=map(int,open(0).read().split());a,c=[0]*-~n,0
for l,r in zip(t[::2],t[1::2]):a[l-1]+=1;a[r]-=1
for i in range(n):a[i+1]+=a[i];c+=a[i]==m
print(c)

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

Solution (Python):
N,M = map(int, input().split())
m0 = 1
m9 = N
for i in range(M):
  L,R = map(int, input().split())
  m0 = max(L, m0)
  m9 = min(R, m9)
print(max(0, m9-m0+1))

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

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

print(ans)

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

L = []
R = []

for i in range(0, M):
    l, r = map(int, input().split())
    L.append(l)
    R.append(r)

ans = min(R) - max(L) + 1
if ans < 0:
    ans = 0

print(ans)

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

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

Solution (Python):
N, M = map(int,input().split())
L = 0
R = 0
Lmax = 0
Rmin = 100000
for i in range(0,M):
  L, R = map(int,input().split())
  if L > Lmax:
    Lmax = L
  if R < Rmin:
    Rmin = R

result = Rmin - Lmax + 1
if result < 0:
    result = 0
print(result)

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

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

print(ans)

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

L = 1
R = N

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

L = R - L + 1
if L < 0:
    L = 0

print(L)

Solution (Python):


import sys

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


def main():
    N, M = i2nn()
    L, R = (0, N)
    for i in range(M):
        Li, Ri = i2nn()
        if L < Li:
            L = Li
        if Ri < R:
            R = Ri
    if L > R:
        print(0)
    else:
        print(R - L + 1)
    return

main()

Solution (Python):
import math

def main():
    n, m = [int(x) for x in input().split(" ")]
    gates = []
    mi = n
    ma = 0

    for x in range(m):
        l, r = [int(x) for x in input().split(" ")]
        gates.append((l, r))

    for gate in gates:
        
        if ma <= gate[0]:
            ma = gate[0]

        if mi >= gate[1]:
            mi = gate[1]

    res = 0
    if mi < ma:
        res = 0
    else:
        res = mi - ma + 1

    print(res)

if __name__ == "__main__":
    main()

Solution (Python):
a = 1

n, m = map(int, input().split())
ans_r = 10 ** 5

l_list = []
r_list = []
for i in range(m):
    l, r = map(int, input().split())
    l_list.append(int(l))
    r_list.append(int(r))
for r in r_list:
    if ans_r > r:
        ans_r = r
for l in l_list:
    if a < l:
        a = l

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

Solution (Python):
l = raw_input().split()
N = int(l[0])
M = int(l[1])

L = []
R = []

for i in range(M):
    l = raw_input().split()
    L.append(int(l[0]))
    R.append(int(l[1]))

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

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

Solution (Python):
N, M = [int(x) for x in input().split()]
L = 1
R = N
for i in range(M):
    L_,R_ = [int(x) for x in input().split()]
    L = max(L, L_)
    R = min(R, R_)

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

Solution (Python):
tmp = input().split()
N = int(tmp[0])
M = int(tmp[1])

max = N
min = 0
for i in range(M):
  inputcard = list(map(int, input().split()))
  if min < inputcard[0]:
    min = inputcard[0]
  
  if max > inputcard[1]:
    max = inputcard[1]
  
if (max - min + 1 < 0):
  print(0)
else:
  print(max - min + 1)

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

Solution (Python):

lst_1 = input().split()

N = int(lst_1[0])
M = int(lst_1[1])

lst_A = []

ans_min = 1
ans_max = N

for i in range(M):
    lst_A.append(input().split())

for m in range(0, M, 1):

    com_min = int(lst_A[m][0])

    if com_min > ans_min:
        ans_min = com_min

    com_max = int(lst_A[m][1])

    if com_max < ans_max:
        ans_max = com_max

ans = ans_max - ans_min + 1

if ans < 1:
    ans = 0

print(ans)

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)
print(max((min(rlist)-max(llist)+1), 0))

Solution (Python):
n,m=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(m)]
max=ab[0][1]
min=ab[0][0]

for i in range(1,m):
    if min<ab[i][0]:
        min=ab[i][0]
    if ab[i][1]<max:
        max=ab[i][1]
if max<min:
    print("0")
else:
    print(max-min+1)

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

Solution (Python):



from sys import stdin

N, M = [int(x) for x in stdin.readline().rstrip().split()]

clear_list = [1] * N
start = 0
end = 0
ans_start = 0
ans_end = 0
    
for i in range(M):
    start, end  = [int(x) for x in stdin.readline().rstrip().split()]
    if ans_start == 0 or ans_start < start : 
        ans_start = start
    if ans_end == 0 or ans_end > end:
        ans_end = end

if ans_end - ans_start < 0:
    print(0)
else:
    print(ans_end - ans_start + 1 )

Solution (Python):
hq_left = []
hq_right = []


listA=[] 
N,M= map(int,input().split())

for i in range(M):
    listA.append(list(map(int,input().split())))


candidate_list = [i for i in range(1,N+1)]

for i in range(M):
    hq_left.append(listA[i][0])
    hq_right.append(listA[i][1])

if min(hq_right)-max(hq_left) <0:
    print(0)
else:
    print(len(candidate_list[max(hq_left):min(hq_right)])+1)

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

Solution (Python):
N, M = map(int, input().split(' '))
 
data = []
l = 1
r = 10**5
for m in range(M):
    li, ri = map(int, input().split(' '))
    if li > l:
      l = li
    if ri < r:
      r = ri
 
if l<= N <= r:
  cnt = (N - l) + 1
elif l > r:
  cnt = 0
elif r < N:
  cnt = (r - l) + 1
else:
  cnt = 0
 
print(cnt)

Solution (Python):
N, M = list(map(int, input().split()))
Ls, Rs = [], []
for i in range(M):
    n, m = list(map(int, input().split()))
    Ls.append(n)
    Rs.append(m)








print(max(0, min(Rs) - max(Ls) + 1))

Solution (Python):
n, m = (int(i) for i in input().split())
ok = [1] * n
l, r = (1,n)
for i in range(1,m+1):
  tmp_l, tmp_r = (int(j) for j in input().split())
  if l < tmp_l:
    l = tmp_l
  if r > tmp_r:
    r = tmp_r

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

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

ans = []
minn = 10000000
maxx = 0
for i in range(m):
    l,r = [int(s) for s in input().split(' ')]
    maxx = max(maxx,l)
    minn = min(minn,r)
if minn - maxx < 0:
    print(0)
else:
    print(minn - maxx+1)

Solution (Python):

n,m = map(int,input().split())
lr = [list(map(int,input().split())) for ii in range(m)]
L = [lr[jj][0] for jj in range(m) ]
R = [lr[jj][1] for jj in range(m) ]

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

Solution (Python):
def i_input(): return int(input())
def s_input(): return map(int,input().split())
def l_input(): return list(map(int, input().split()))

N, M = s_input()
L, R = [], []

for i in range(M):
    Li, Ri = s_input()
    L.extend([Li])
    R.extend([Ri])

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

num = R_min - L_max

if num >= 0:
    print(num + 1)
else:
    print(0)

Solution (Python):
N, M = list(map(int, input().split()))
Ls, Rs = [], []
for i in range(M):
    n, m = list(map(int, input().split()))
    Ls.append(n)
    Rs.append(m)








print(max(0, min(Rs) - max(Ls) + 1))

Solution (Python):
n,m=map(int,input().split(" "))
l_init,r_init=map(int,input().split(" "))
ans_l=l_init
ans_r=r_init
isPossible=True
for i in range(m-1):
	l,r=map(int,input().split(" "))
	isupdated=False
	next_ans_l=ans_l
	next_ans_r=ans_r
	if ans_l <= l and l <= ans_r:
		next_ans_l=l
		isupdated=True
	if ans_l <= r and r <= ans_r:
		next_ans_r=r
		isupdated=True
	if l <= ans_l and ans_r <= r:
		isupdated=True
	if not isupdated:
		isPossible=False
		break
	ans_l=next_ans_l
	ans_r=next_ans_r
ans=ans_r-ans_l
if isPossible:
	print(ans+1)
else:
	print(0)

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

a = []
for _ in range(n):
    a.append(0)

for _ in range(m):
    l,r = map(int, input().split())
    a[l-1] += 1
    if r != n:
        a[r] -= 1

for i in range(n):
    x += a[i]
    if(x == m):
        c += 1

print(c)

Solution (Python):
import copy
N, M = map(int, input().split())
LR = []
LR2 = []
count = 0
min = 1
max = N
for i in range(M):
    l, r = map(int, input().split())
    LR2.append(l)
    LR2.append(r)
    LR3 = copy.copy(LR2)
    LR.append(LR3)
    LR2.clear()


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

Solution (Python):
list_l = []
list_R = []
ID,gate = map(int,input().split())
s = 0
for i in range(gate):
  l,r = map(int,input().split())
  list_l.append(l)
  list_R.append(r)
a = max(list_l)
b = min(list_R)

for i in range(ID+1):
  if a <= i <= b:
    s += 1
  else:
    None
print(s)

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

stepper = 0
gate = []

for j in range(M):
    gate_1, gate_2 = map(int, input().split())
    gate.append([gate_1, gate_2])

gate_min = 0
gate_max = N

for m in range(M):
    if gate[m][0] > gate_min:
        gate_min = gate[m][0]

    if gate[m][1] < gate_max:
        gate_max = gate[m][1]


for k in range(N):
    k = k+1
    if gate_min <= k <= gate_max:
        stepper += 1

print(stepper)

Solution (Python):
N,M = map(int, input().split())
X = [0]*(N+2)
for i in range(M):
    l,r = map(int, input().split())
    X[l] += 1
    X[r+1] -= 1

for i in range(N):
    X[i+1] += X[i]
ans = X.count(M)
print(ans)

Solution (Python):
n,m = map(int,input().split())
gates = [list(map(int,input().split())) for _ in range(m)]
l = []
r = []
for gate in gates:
    l.append(gate[0])
    r.append(gate[1])
ans = min(r)+1 - max(l)
print(ans if ans > 0 else 0)

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

l_max=-1
r_min=float("inf")
for i in range(M):
	l,r=map(int, raw_input().split())
	l_max=max(l_max, l)
	r_min=min(r_min, r)

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

Solution (Python):
n,m = map(int, input().split())
a = []
b = []
c = []
for i in range(m):
  a.append(list(map(int, input().split())))
for i in range(m):
  b.append(a[i][0])
  c.append(a[i][1])
  
b_max = max(b)
c_min = min(c)

ans = max(0, min(c) - max(b) + 1)

print(ans)

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

Solution (Python):
N, M = map(int, input().split())
table = [[int(i) for i in input().split()] for m in range(M)]
L=[]
R=[]
c=0
d=0
x=0
for i in range(M):
  L.append(table[i][0])
  
for i in range(M):
  R.append(table[i][1])
  
c=max(L)
d=min(R)
    


if c>d:
    print("0")
else:
    x=d-c+1
    print("%d"%x)

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)

s = 1
e = N

for l,r in zip(Ls, Rs):
    s = max(s, l)
    e = min(e, r)
    if s > e:
        break
print(max(0, (e+1)-s))

Solution (Python):
a,b=map(int, input().split())
c,d=map(int, input().split())
start=c
end=d


  
  

for i in range(b-1):
  e,f=map(int, input().split())
  if e>=start:
    start=e
  if f<=end:
    end=f

if end>= start:
  out=end-start+1
else:
  out=0
  
  

print(out)

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

Solution (Python):
a = list(map(int,input().split()))
b = [0]*2
c = [0]*2
s = 0
for i in range(a[1]):
  if i == 0:
    b[0],c[0] = map(int,input().split())
  else:
    b[1],c[1] = map(int,input().split())
    if (b[1] > c[0]) or (c[1] < b[0]):
      s += 1
      break
    else:
      if b[1] >= b[0]:
        b[0] = b[1]
      if c[1] <= c[0]:
        c[0] = c[1]
if s == 0:
  print(c[0]-b[0]+1)
else:
  print(0)

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

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

Lmax = 0
Rmin = 1000000
for i in range(m):
	L,R = map(int,input().split())
	if Lmax < L:
		Lmax = L
	if Rmin > R:
		Rmin = R
res = Rmin - Lmax + 1
if res > 0:
	print("%d" % (Rmin - Lmax +1))
else:
	print(0)

Solution (Python):
def main():
    N, M = map(int, input().split())
    L = []
    R = []
    for m in range(M):
        l, r = map(int, input().split())
        L.append(l)
        R.append(r)
    print(max((min(R)-max(L)+1), 0))


if __name__ == '__main__':
    main()

Solution (Python):
N,M=map(int,input().split())
Lm=0
Rm=N
ans=0

for i in range(M):
    Lt,Rt=map(int,input().split())
    if Lm < Lt:
        Lm = Lt
    if Rm > Rt:
        Rm = Rt
if Rm < Lm:
    print(0)
else:
    print(Rm-Lm+1)

Solution (Python):
def spaceinput():
    return list(map(int,input().split(" ")))


def merge(a,b):
    l=sorted([a[0],b[0]])
    r=sorted([a[1],b[1]])
    return l[1],r[0]


N,M=spaceinput()
gates=[]
for i in range(M):
    gates.append(spaceinput())

d=gates[0]
for i in range(1,M):
    d=merge(d,gates[i])

print(max(0,d[1]-d[0]+1))

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

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

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

Solution (Python):
def main():
    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 rmin - lmax + 1 <= 0:
        ans = 0
    else:
        ans = rmin - lmax + 1
    print(int(ans))


main()

Solution (Python):
N, M = map(int, input().split())
i = 1
j = N
for m in range(M):
    l, r = map(int, input().split())
    i = max(i,l)
    j = min(j,r)
ans = j + 1 - i
if ans < 0:
    print(0)
else:
    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
N,M = map(int,input().split())
lr =[[int(i)for i in input().split()]for _ in range(M)]
ans =[0,N+1]
for LR in lr:
    if ans[0] < LR[0]:
        ans[0]=LR[0]
    if LR[1] < ans[1]:
        ans[1] =LR[1]
print(max(0,ans[1]-ans[0]+1))

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

top = min(b) 
under = max(a) 

if top >= under:
    ans = top - under + 1
if top < under:
    ans = 0

print(ans)

Solution (Python):

N,M = map(int, input().split())
LR_list=[]
Card_list=[]

Min_L = 1
Max_R = N


for i in range(M):
  L,R = map(int, input().split())
  if L>Min_L:
    Min_L=L
  if Max_R>R:
    Max_R=R

if Max_R >= Min_L:
  print(Max_R - Min_L + 1)
else:
  print(0)

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

for x in range(M-1):
    L, R = map(int, input().split())
    if L > L1:
        L1 = int(L)
    if R < R1:
        R1 = int(R)

if R1 == L1:
    print("1")
if L1 > R1:
    print("0")
if L1 < R1: 
	print(R1-L1+1)

Solution (Python):

if __name__=='__main__':
  N, M = map(int, input().split())
  L, R = 1, 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(0)
  else:
    print(R-L+1)

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

a = r_p - l_p + 1

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

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

l = 1
r = N
flag = False
for i in range(M):
    l_1, r_1 = list(map(int, input().split()))
    if l_1 > l:
        l = l_1
    if r_1 < r:
        r = r_1
    if l > r:
        flag = True
        break
    
if flag:
    print(0)
else:
    print(r - l + 1)

Solution (Python):
Ns,Ms=input().split()
N=int(Ns)
M=int(Ms)
lists=[]
ansmins,ansmaxs=input().split()
ansmin=int(ansmins)
ansmax=int(ansmaxs)
for i in range(M-1):
	tmpmin,tmpmax=input().split()
	if int(tmpmin)>ansmin:
		ansmin=int(tmpmin)
	if int(tmpmax)<ansmax:
		ansmax=int(tmpmax)

if (ansmax-ansmin+1)<0:
	ans=0
else:
	ans=ansmax-ansmin+1
print(ans)

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

for i in range(m):
    left.append(lr[i][0])
    right.append(lr[i][1])

ans = min(right) - max(left) + 1

if ans < 0:
    ans = 0

print(ans)

Solution (Python):
M,N = map(int,input().split())
ans=set(range(1,M+1))
START=0
END=10**5
for s in range(N):
  S,Z = map(int,input().split())
  if START <= S:
    START = S
  if END >= Z:
    END = Z
ans = range(START,END+1)
print(len(ans))

Solution (Python):
def calc():
    i_list = input().split()
    n = int(i_list[0])
    m = int(i_list[1])

    min_card = 1
    max_card = n

    for i in range(m):
        i_list = input().split()
        l = int(i_list[0])
        r = int(i_list[1])
        if l > min_card:
            min_card = l
        if r < max_card:
            max_card = r

    ok_num = max_card - min_card + 1
    if ok_num > 0:
        print(ok_num)
    else:
        print(0)

if __name__ == '__main__':
    calc()

Solution (Python):
from itertools import accumulate

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

q = [0] * (N+1)
for l, r in LR:
    
    q[l-1] += 1
    q[r] -= 1



print(sum(M == m for m in list(accumulate(q))[:-1]))

Solution (Python):

import numpy as np
N, M = map(int, input().split())
L_list = []
R_list = []
for i in range(M):
    L, R = map(int, input().split())
    L_list.append(L)
    R_list.append(R)
if min(R_list) - max(L_list) < 0:
    print(0)
else:
    print(min(R_list) - max(L_list)+1)

Solution (Python):
N, M = [int(x) for x in input().split(' ')]

L = 0
R = N
for i in range(M):
  l,r = [int(x) for x in input().split(' ')]
  if L < l:
    L = l
  if r < R:
    R = r

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

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

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

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

Solution (Python):
import bisect
from operator import itemgetter
import math
import functools
import itertools
import numpy as np
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,M = IL()

LR = [IL() for i in range(M)]

l = 0
r = MAX_INT
for L,R in LR:
    if l < L:
        l = L
    if r > R:
        r = R
print(max(0,r-l+1))

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

start, end = e[0][0], e[0][1]
for i in e: 
  if start < i[0]:
    start = i[0]
  if end > i[1]:
    end = i[1]
  if start > end:
    break

if start > end:
  print(0)
else:
  print(end-start+1)

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

n,m=map(int,input().split())
leftgate=[]
rightgate=[]
for i in range(m):
  l,r=map(int,input().split())
  leftgate.append(l)
  rightgate.append(r)
if (min(rightgate)-max(leftgate))>-1:
  print(min(rightgate)-max(leftgate)+1)
else:
  print(0)