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())
mi = 0
ma = N
for i in range(M):
    l, r = map(int, input().split())
    if mi < l:
        mi = l
    if ma > r:
        ma = r
if ma < mi:
    print(0)
else:
    print(ma-mi+1)

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

lefts = list()
rights = list()
for i in range(m):
    left, right = map(int, input().split())
    lefts.append(left)
    rights.append(right)

ans = min(rights) - max(lefts) + 1
if ans >= 0:
    print(ans)
else:
    print(0)

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

Solution (Python):
n,m=map(int,input().split())
l=[0 for i in range(n+1)]
for i in range(m):
  a,b=map(int,input().split())
  l[a-1]+=1
  l[b]-=1
s=0
c=0
for i in l:
  s+=i
  if s==m:
    c+=1
print(c)

Solution (Python):
N,M = map(int,input().split())
lr = []
left,right = map(int,input().split())
for i in range(1,M):
    l,r = map(int,input().split())

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

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

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


Lmax=1
Rmin=N
for i in range(M):
	if L[i]>Lmax: Lmax=L[i]
	if R[i]<Rmin: Rmin=R[i]

if Lmax>Rmin: print(0)
else: print(Rmin-Lmax+1)

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

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

Solution (Python):
from collections import deque
from heapq import heappush,heappop
import re

def int_raw():
    return int(input())

def ss_raw():
    return input().split()

def ints_raw():
    return list(map(int, ss_raw()))

INF = 1<<29



def main():
    N, M = ints_raw()
    rower = 1
    higher = N
    for _ in range(M):
        l,r = ints_raw()
        rower = max(l,rower)
        higher = min(higher,r)
    return max(higher-rower+1,0)
            
print(main())

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()
 
n,m = na()
a=1
b=n
for i in range(m):
    l,r = na()
    a=max(a,l)
    b=min(b,r)

print(max(b-a+1,0))

Solution (Python):
a,b = map(int,input().split())
lower=0
upper=1000000000000000
for i in range(b):
    c,d = map(int,input().split())
    if c>lower:
        lower=c
    if d<upper:
        upper=d

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

Solution (Python):
list=input().split(" ")
n=int(list[0])
m=int(list[1])

hidari=0
migi=n+1

for i in range(m):
    list=input().split(" ")
    x=int(list[0])
    y=int(list[1])
    if x>hidari:
        hidari=x
    if y<migi:
        migi=y


print(max(migi-hidari+1,0))

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

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

lm = max([l for l, r in LR])
rm = min([r for l, r in LR])

print(max(rm - lm + 1, 0))

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

Solution (Python):

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

low = 1
high = n

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

    low = max(low,l)
    high = min(high,r)


ans = high-low + 1

if ans < 0:
    ans = 0

print(ans)

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

lm,rm = 0,n+1
for i in range(m):
  l,r = [int(it) for it in input().split()]
  lm = max(l,lm)
  rm = min(r,rm)
s = max(rm - lm + 1,0)
print ( s )

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

Solution (Python):

import sys
from collections.abc import Iterable
from math import *
from itertools import *
from collections import *
from functools import *
from operator import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    maxl = -float('inf')
    minr = float('inf')
    for l, r in zip(L, R):
        maxl = max(l, maxl)
        minr = min(r, minr)
    
    return max(0, (minr - maxl + 1))


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    M = int(next(tokens))  
    L = [int()] * (M)  
    R = [int()] * (M)  
    for i in range(M):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))
    result = solve(N, M, L, R)
    if isinstance(result, Iterable) and not isinstance(result, str):
        result = '\n'.join([str(v) for v in result])
    print(result)

if __name__ == '__main__':
    main()

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

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

print(max(Rmin - Lmax + 1, 0))

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

Lmax = 1
Rmin = N
for m in range(M):
    L, R = list(map(int, input().split()))
    Lmax = max(Lmax, L)
    Rmin = min(Rmin, R)

print(max(0, Rmin - Lmax + 1))

Solution (Python):
n,m= map(int,input().split())
s = [list(map(int,input().split())) for i in range(m)]
l = 0
r = 10 ** 18
for i in range(m):
    if l < s[i][0]:
        l = s[i][0]
    if s[i][1] < r:
        r = s[i][1]
if r-l < 0:
    print(0)
else:
    print(r-l+1)

Solution (Python):
N, M = (int(i) for i in input().split())

lmin = []
rmax = []
for i in range(M):
    L, R = (int(i) for i in input().split())
    lmin.append(L)
    rmax.append(R)

res = min(rmax) - max(lmin) + 1 
if res < 0:
    print(0)
else:
    print(res)

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

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

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

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

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

l_lst = []
r_lst = []
for i in range(M):
	l,r = map(int,input().split())
	l_lst.append(l)
	r_lst.append(r)

l = max(l_lst)
r = min(r_lst+[N])

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

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

l=0
r = 10**9
for g in gates:
    l = max(l, g[0])
    r = min(r, g[1])
print(r-l+1 if r>=l else 0)

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

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

for _ in range(m):
    l, r = [int(i) for i in input().split()]
    if l > l_max:
        l_max = l
    if r < r_min:
        r_min = r

print(max(r_min - l_max + 1, 0))

Solution (Python):

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

l_max = 0
r_min = n+1
for i in range(m):
    a, b = [int(i) for i in input().split()]
    l_max = max(l_max, a)
    r_min = min(r_min, b)
print(max(0, r_min - l_max + 1))

Solution (Python):
import sys
from itertools import accumulate

def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    acc = [0] * (N+1)
    for _ in range(M):
        left, right = map(int, input().split())
        acc[left-1] += 1
        acc[right] -= 1

    cnt = 0
    for a in accumulate(acc):
        if a == M:
            cnt += 1

    return cnt


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

Solution (Python):


def solve():
    N, M = list(map(int, input().split()))

    l_max = 0
    r_min = float("inf")
    for i in range(M):
        l, r = list(map(int, input().split()))
        l_max = max(l_max, l)
        r_min = min(r_min, r)

    print(max(r_min-l_max+1, 0))    



if __name__ == "__main__":
    solve()

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

min_id = 0
max_id = N+1
for i in range(M):
    l, r = map(int, input().split())
    if min_id < l:
        min_id = l
    if max_id > r:
        max_id = r

result = max_id - min_id
if result < 0:
    print(0)
else:
    print(result+1)

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

ans = 0
for ii in imos:
    if ii==M:
        ans += 1
print(ans)

Solution (Python):
n, m = map(int, input().split())
a = [0 for i in range(n + 1)]
for i in range(m):
    L, R = map(int, input().split())
    L -= 1
    R -= 1
    a[L] += 1
    a[R + 1] -= 1
a_t = [0]
for i in range(n + 1):
    a_t.append(a[i] + a_t[i])
ans = 0
for i in range(n + 2):
    if a_t[i] == m:
        ans += 1
print(ans)

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

Solution (Python):
N,M = map(int,input().split())
x = [0 for i in range(N+2)]
for m in range(M):
    L,R = map(int,input().split())
    x[L] += 1
    x[R+1] -=1

c = 0
cnt = 0
for i in range(N+2):
    c += x[i]
    if c == M:
        cnt += 1


print(cnt)

Solution (Python):

import re
import bisect
from datetime import datetime as dt
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)
print(max(0, minR - maxL + 1))

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

ans = [0]*(N+1)

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

for i in range(1, N):
    ans[i] += ans[i-1]

print(ans[:-1].count(M))

Solution (Python):
import sys

from fractions import gcd





from itertools import accumulate













input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())



def iterate_tokens():
    for line in sys.stdin:
        for word in line.split():
            yield word


if __name__ == '__main__':

    tokens = iterate_tokens()
    N = int(next(tokens))  
    M = int(next(tokens))  
    L = [int()] * (M)  
    R = [int()] * (M)  
    for i in range(M):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))

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

Solution (Python):
N,M=map(int,input().split())
s,t=map(int,input().split())
for i in range(M-1):
  a,b=map(int,input().split())
  s=max(s,a)
  t=min(t,b)
if t>=s:
  print(t-s+1)
else:
  print(0)

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

le = 0
ri = N - 1
for i in range(M):
    L, R = map(int, input().split())
    L -= 1
    R -= 1
    le = max(le, L)
    ri = min(ri, R)

if (le <= ri):
    print(ri - le + 1)
else:
    print(0)

Solution (Python):
N,M=map(int,input().split())
R=10**5
L=0
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):


import itertools
from collections import Counter
from collections import defaultdict
import bisect

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

    max_l = 0
    min_r = 10 ** 6
    for _ in range(M):
        l, r = map(int, input().split())
        max_l = max(max_l, l)
        min_r = min(min_r, r)

    print(max(0, min_r - max_l + 1))

if __name__ == '__main__':
    main()

Solution (Python):
import collections as c
ip = lambda : map(int, input().split())


N,M = ip()
L,R = ip()
for i in range(1,M):
    l,r = ip()
    if R<l or r<L:
        print(0)
        exit()
    L = max(L,l)
    R = min(R,r)
print(R-L+1)

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

pl, pr = 1, N
for l, r in LR:
	pl = max(pl, l)
	pr = min(pr, r)

print(max(0, pr - pl + 1))

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

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 = -1
r = N + 1
for i in range(M):
    L, R = map(int, input().split())
    l = max(l, L)
    r = min(r, R)
    
if l > r:
    print(0)
else:
    print(r - l + 1)

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

l_max = -1
r_min = pow(10, 6)

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

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

Solution (Python):
def solve():

  n,m = (int(i) for i in input().split())
  ans = 0
  left = 0
  right = 10**9
  for i in range(m):
    l,r = (int(i) for i in input().split())
    left = max(left,l)
    right = min(right,r)
  if right <left :
    print(0)
  else:
   print(1+right-left)
    
solve()

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

for _ in range(M):
    tL, tR = [int(i) for i in input().split()]
    if tL > L:
        L = tL
    if tR < R:
        R = tR

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

Solution (Python):

import sys


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    print(max(0,min(R) - max(L) + 1))



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    M = int(next(tokens))  
    L = [int()] * (M)  
    R = [int()] * (M)  
    for i in range(M):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))
    solve(N, M, L, R)

if __name__ == '__main__':
    main()

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

ansL = 0
ansR = float('inf')
for _ in range(M):
  l, r = map(int, input().split())
  ansL = ansL if ansL > l else l
  ansR = ansR if ansR < r else r

print(max(0, ansR - ansL + 1))

Solution (Python):

import sys


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    lm = max(L)
    rm = min(R)
    print(max(0,rm-lm+1))

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    M = int(next(tokens))  
    L = [int()] * (M)  
    R = [int()] * (M)  
    for i in range(M):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))
    solve(N, M, L, R)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N, M = [int(n) for n in input().split()]
    memo = [0]*N
    for i in range(M):
        l, r = [int(n)-1 for n in input().split()]
        memo[l] += 1
        if  r+1 < N:
            memo[r+1] -= 1
    for i in range(N-1):
        memo[i+1] += memo[i]
    res = 0
    for i in range(N):
        if memo[i] == M:
            res += 1
    print(res)
main()

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

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

ans = y-x+1
if ans < 0:
    print (0)
else:
    print (ans)

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)

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

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

ans = min(B)-max(A)+1
if ans <= 0:
    ans = 0
print(ans)

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

L, R = [], []
for i in range(M):
    l, r = [int(c) for c in input().split()]
    L.append(l)
    R.append(r)

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

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

a=[list(map(int,input().split())) for i in range(M)]
a1=[a[i][0] for i in range(M)]
a2=[a[i][1] for i in range(M)]

ans=max(0,min(a2)-max(a1)+1)
print(ans)

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

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

Solution (Python):
import math

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

l=[]
r=[]

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

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

Solution (Python):

def main():
    n, m = map(int, raw_input().split())
    min_id = 1
    max_id = n
    for _ in xrange(m):
        l, r = map(int, raw_input().split())
        min_id = max(min_id, l)
        max_id = min(max_id, r)
    print max(0, max_id - min_id + 1)


if __name__ == '__main__':
    main()

Solution (Python):
N, M = map(int, input().split())
L = 0
R = 10**6
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 R >= L else 0
print(ans)

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

Solution (Python):
import sys
from collections import Counter

input = sys.stdin.readline

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

A = [0] * (N + 1)
for _ in range(M):
    L, R = map(int, input().split())
    A[L - 1] += 1
    A[R] += -1

for i in range(1, N):
    A[i] += A[i - 1]

c = Counter(A)
print(c[M])

Solution (Python):

from __future__ import division, print_function

import os
import sys
from __builtin__ import xrange as range
from cStringIO import StringIO
from future_builtins import ascii, filter, hex, map, oct, zip
from io import IOBase

import __pypy__


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

    cnt, res = 0, 0
    for i in range(n + 2):
        cnt += cards[i]
        if cnt == m:
            res += 1

    print(res)




BUFSIZE = 8192


class FastI(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = StringIO()
        self.newlines = 0

    def read(self):
        if self._buffer.tell():
            return self._buffer.read()
        return os.read(self._fd, os.fstat(self._fd).st_size)

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count("\n") + (not b)
            ptr = self._buffer.tell()
            self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr)
        self.newlines -= 1
        return self._buffer.readline()


class FastO(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = __pypy__.builders.StringBuilder()
        self.write = lambda s: self._buffer.append(s)

    def flush(self):
        os.write(self._fd, self._buffer.build())
        self._buffer = __pypy__.builders.StringBuilder()


def print(*args, **kwargs):
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastI(sys.stdin), FastO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")



if __name__ == "__main__":
    main()

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

Solution (Python):
n,m=map(int,input().split())
data=[0]*(10**5+10)
for i in range(m):
    l,r=map(int,input().split())
    data[l]+=1
    data[r+1]-=1
ans=0
for i in range(len(data)-1):
    data[i+1]=data[i+1]+data[i]
    if data[i+1]==m:
        ans+=1
print(ans)

Solution (Python):

import sys
INF = float("inf")


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    print(max(min(R)-max(L)+1, 0))
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    M = int(next(tokens))  
    L = [int()] * (M)  
    R = [int()] * (M)  
    for i in range(M):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))
    solve(N, M, L, R)


if __name__ == '__main__':
    main()

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

Solution (Python):
n,m=map(int,input().split())
a=[]
l=[]
r=[]
for i in range(m):
    a.append(list(map(int,input().split())))
    l.append(a[i][0])
    r.append(a[i][1])

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

Solution (Python):
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
l, r = 0, 10 ** 5 + 1
for i in range(M):
    L, R = map(int, input().split())
    if L > l:
        l = L
    if R < r:
        r = R
print(max(0, r - l + 1))

Solution (Python):
N,M=[int(a) for a in input().split()]
LR=[[int(a) for a in input().split()] for _ in range(M)]
L1,R1=-10**10,10**10
for L, R in LR:
    L1 = max(L1, L)
    R1 = min(R1, R)
print(max(R1-L1+1, 0))

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

left = 1
right = N

for l, r in LR:
    left = max(left, l)
    right = min(right, r)
    if left > right:
        break
print(max(right - left + 1, 0))

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

Solution (Python):
N, M = map(int, input().split())
l = 1
r = 10 ** 6
for i in range(M):
    lr = list(map(int, input().split()))
    l = max(l, lr[0])
    r = min(r, lr[1])
ans = 0
for i in range(N):
    if l <= i + 1 <= r:
        ans += 1
print(ans)

Solution (Python):
import sys
import math
from itertools import permutations
input = sys.stdin.readline

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

arr=[1,N]
flag=True
for i in range(M):
	l,r=map(int,input().split())
	if flag:
		arr[0]=max(l,arr[0])
		arr[1]=min(r,arr[1])
	if arr[1]<arr[0]:
		flag=False

if flag:
	print(arr[1]-arr[0]+1)
else:
	print(0)

Solution (Python):
N,M = map(int,input().split())
L,R = map(int,input().split())
pos = True
for i in range(M-1):
    L_,R_ = map(int,input().split())
    if R < L_ or R_ < L:
        pos = False
    L = max(L,L_)
    R = min(R,R_)
if pos:
    print(R-L+1)
else:
    print(0)

Solution (Python):
n,m=map(int,input().split())
A,B=[0]*(n+1),[0]*(n+1)
for i in range(m):
  l,r=map(int,input().split())
  A[l-1]+=1
  A[r]-=1
B[0]=A[0]
for i in range(1,n):
  B[i]=B[i-1]+A[i]
print(B.count(m))

Solution (Python):
n, m = [int(x) for x in raw_input().split()]
a = []
al, ar = 1, n
for _ in xrange(m):
    l, r = [int(x) for x in raw_input().split()]
    a.append((l, r))
a.sort()
for (l, r) in a:
    al = max(l, al)
    ar = min(r, ar)
ans = max(ar - al + 1, 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)
x = min(R) - max(L)
if x < 0:
	print(0)
else:
	print(x + 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())
print(max(0,min(r)-max(l)+1))

Solution (Python):
import math

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

L=1
R=N
for i in range(M):
    tL,tR = map(int, input().split())
    if tL > L:
        L = tL
    if tR < R:
        R = tR

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

print(Ans)

Solution (Python):
from operator import itemgetter

N,M = [int(x) for x in input().split()]
LR = [[int(x) for x in input().split()]for _ in range(M)]
max_l = max(LR,key = itemgetter(0))[0]
min_r = min(LR,key = itemgetter(1))[1]
print(max(min_r-max_l+1,0))

Solution (Python):
n,m=map(int,input().split())
l=[]
min=0
max=n
for i in range(m):
    x,y=map(int,input().split())
    if min<x:
        min=x
    if y<max:
        max=y

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

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

passable = [0] * (n+1)
for l, r in lr:
    passable[l-1] += 1
    passable[r] -= 1
for i in range(n):
    passable[i+1] += passable[i]
print(passable[:n].count(m))

Solution (Python):
from itertools import accumulate

N, M = map(int, input().split())
LRs = [tuple(map(int, input().split())) for _ in range(M)]

cards = [0]*N
for L, R in LRs:
    cards[L-1] += 1
    if R < N:
        cards[R] -= 1

cards = list(accumulate(cards))

print(cards.count(M))

Solution (Python):
n, m = map(int, input().split())
ll = 1
rr = n
for i in range(m):
  l, r = map(int, input().split())
  ll = max([ll, l])
  rr = min([rr, r])

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

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

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

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

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

Ls = []
Rs = []
for i in range(M):
    L, R = [int(elm) for elm in input().split(" ")] 
    Ls.append(L)
    Rs.append(R)
    
max_L = max(Ls)
min_R = min(Rs)


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

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

maxL = max(L)
minR = min(R)

sa = minR - maxL + 1

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

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

Solution (Python):
read = input 
rn = lambda :map(int, read().split())
n,m = rn()
s = [0 for i in range(n + 10000)]
for i in range(m):
    l,r = rn()
    s[l] += 1
    s[r+1] -= 1
for i in range(1, len(s) - 10):
    s[i] += s[i-1]
ans = 0
for i in range(1, len(s) - 10):
    if s[i] == m:
        ans+=1
print(ans)

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

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(l, lmax)
    rmin = min(r, rmin)

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

Solution (Python):
N, M = map(int, input().split())
Lmax = 0
Rmin = 10 ** 5
for i in range(M):
  l, r = map(int, input().split())
  Lmax = max(l, Lmax)
  Rmin = min(r, Rmin)
print(max(Rmin - Lmax + 1, 0))

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

ans = [0] * (n + 2)

for i in range(m):

    l, r = map(int, input().split())

    ans[l] += 1

    ans[r + 1] -= 1

count = 0

for i in range(n + 1):

    ans[i + 1] += ans[i]

    if ans[i] == m:
        count += 1

print(count)

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

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

mi = -1
ma = 100000000000
for i in range(M):
    l,r = map(int, input().split())
    mi = max(l, mi)
    ma = min(ma, r)

if ma >= mi:
    print(ma-mi+1)
else:
    print(0)

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

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

count = 0
for i in range(n):
    if memo[i] == m:
        count += 1

print(count)

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

Solution (Python):
n, m = [int(item) for item in input().split()]
la, ra = -float('inf'), float('inf')
for i in range(m):
    l, r = [int(item) for item in input().split()]
    la = max(l, la)
    ra = min(r, ra)
print(max(0, ra - la + 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 II(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n,m=LI()
  l1=[]
  l2=[]
  for i in range(m):
    a,b=LI()
    l1.append(a)
    l2.append(b)

  l1.sort()
  l2.sort()

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

main()

Solution (Python):
N, M = map(int, input().split())
maxl, minr = 0, N
for _ 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):
from itertools import accumulate
def inpl(): return map(int, input().split())

N, M = inpl()
X = [0]*(N+1)
for _ in range(M):
    l, r = inpl()
    X[l-1] += 1
    X[r] -= 1
S = list(accumulate(X))
print(sum([s == M for s in S]))