CodeEff/ECCO huggingface dataset:
# Original solution
n, m = list(map(int, input().split()))
row1 = list(map(int, input().split()))
lis = list(range(row1[0], row1[1]+1))
#print(lis)

for i in range(m-1):
  rowi = list(map(int, input().split()))
  rowi = list(range(rowi[0], rowi[1]+1))
  newlis = []
  for j in lis:
    #print(j)
    if j in rowi:
      newlis.append(j)
    #print(newlis)
  lis = newlis
      
print((len(lis)))

# Optimized solution
n, m = list(map(int, input().split()))
low =[]
high=[]

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

if minhigh-maxlow >= 0:
  print((minhigh-maxlow+1))
else:
  print((0))
# Original solution
from collections import Counter

N, M = list(map(int, input().split()))
range_dict = Counter()
for _ in range(M):
    l, r = list(map(int, input().split()))
    for i in range(l, r+1):
        range_dict[i] += 1

print((len([v for k, v in range_dict.most_common() if v==M])))


# Optimized solution
from collections import Counter

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

print((min_r - max_l + 1 if min_r - max_l >= 0 else 0))
# Original solution
# input
N, M = list(map(int, input().split()))
check = []
c_append = check.append
for i in range(1, M + 1):
    L, R = list(map(int, input().split()))
    c = set(list(range(L, R + 1)))
    if c not in check:
        c_append(c)

# check
mastar_keys = set(list(range(1, N + 1)))
for c in sorted(check, key=lambda x: len(x)):
    mastar_keys = mastar_keys & c
    if len(mastar_keys) == 0:
        break

print((len(mastar_keys)))

# Optimized solution
# input
N, M = list(map(int, input().split()))
mastar_keys = set(list(range(1, N + 1)))
check = []
c_append = check.append
for i in range(1, M + 1):
    L, R = list(map(int, input().split()))
    c = set(list(range(L, R + 1)))
    mastar_keys = mastar_keys & c

print((len(mastar_keys)))
# Original solution
# input
N, M = list(map(int, input().split()))
mastar_keys = set(list(range(1, N + 1)))
check = []
c_append = check.append
for i in range(1, M + 1):
    L, R = list(map(int, input().split()))
    c = set(list(range(L, R + 1)))
    mastar_keys = mastar_keys & c

print((len(mastar_keys)))


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

print((len(list(range(x, y + 1)))))
# Original solution
n,m = list(map(int,input().split()))
l = list(list(map(int,input().split())) for _ in range (m))
p = set(i for i in range(1,n+1))
for i in l:
  s,t = i
  a = set(q for q in range(s,t+1))
  p = p&a
print((len(p)))

# Optimized solution
n,m = list(map(int,input().split()))
l = list(list(map(int,input().split())) for _ in range (m))
a = l[0][0]
b = l[0][1]
for s,t in l:
  a = max(a,s)
  b = min(b,t)
print((max(b-a+1,0)))
# Original solution
# -*- Coding: utf-8 -*-

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

list_in = []

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

count = 0
for i in range(N):
	flag = 0
	for j in range(M):
		if ((i+1)<list_in[j][0]) | ((i+1)>list_in[j][1]):
			flag = 1
			break

	if flag==1:
		flag = 0
	else:
		count += 1

print(count)

# Optimized solution
# -*- Coding: utf-8 -*-

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

L = 0
R = N

for i in range(M):
	li,ri = list(map(int,input().split()))
	L = max(L,li)
	R = min(R,ri)

print(max(0,R-L+1))
# Original solution
n,m = list(map(int,input().split()))

tmp = set()

for val in range(m):
    l,r = list(map(int,input().split()))
    tmp = set(list(range(l,r+1)))
    if val ==0:
        seki = tmp
    else:
        seki = seki & tmp

print((len(list(seki))))


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

l_L,r_L = [],[]

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

    l_L.append(l)
    r_L.append(r)

r = min(r_L)
l = max(l_L)

if r-l >= 0:
    print((r-l+1))
else:
    print((0))
# Original solution
def main():
    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)

    key_set = {i for i in range(L[0], R[0]+1)}
    for i in range(1, M):
        key_set = key_set & {i for i in range(L[i], R[i]+1)}

    print((len(key_set)))


if __name__ == '__main__':
    main()

# Optimized solution
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)

ans = min(R) - max(L) + 1
if ans >= 0:
    print(ans)
else:
    print((0))
# Original solution
n,m = list(map(int,input().split()))
ans = [0 for _ in range(n)]
for i in range(m):
    l,r = list(map(int,input().split()))
    l-=1;r-=1
    for j in range(l,r+1):
        ans[j] += 1
print((ans.count(m)))

# Optimized solution
n,m = list(map(int,input().split()))
ans = [i+1 for i in range(n+1)]
l = []
r = []
for i in range(m):
    l1,r1 = list(map(int,input().split()))
    l.append(l1)
    r.append(r1)
    
left = max(l)
right = min(r)
print((len(ans[left:right+1])))
# Original solution
N, M = list(map(int, input().split()))
cards = [0 for x in range(N)]
sum = 0
for x in range(M):
    l, m = list(map(int, input().split()))
    for y in range(l - 1, m):
        cards[y] = cards[y] + 1
        if x == M - 1 and cards[y] == M:
            sum = sum + 1
print(sum)

# Optimized solution
N, M = list(map(int, input().split()))
maxL = 0
minM = N
for x in range(M):
    l, m = list(map(int, input().split()))
    maxL = max(maxL, l)
    minM = min(minM, m)

print((max(minM - maxL + 1, 0)))
# Original solution
N, M = list(map(int,input().split()))

cards = [1 for i in range(N)]

for i in range(M):
    L, R = list(map(int,input().split()))
    for i in range(0,L-1):
        cards[i] = 0

    for i in range(R,N):
        cards[i] = 0


print((sum(cards)))



# Optimized solution
N, M = list(map(int,input().split()))
minL = 1
maxR = N

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

    if R < maxR:
        maxR = R

if maxR-minL+1 > 0:
    print((maxR-minL+1))
else:
    print((0))
# Original solution
N,M=list(map(int,input().split()))
L=[]
R=[]
for _ in range(M):
    l,r=list(map(int,input().split()))
    L.append(l)
    R.append(r)
L.sort()
R.sort()
print((0 if R[0]<L[-1] else R[0]-L[-1]+1))

# Optimized solution
N,M=list(map(int,input().split()))
L=0
R=10**9
for _ in range(M):
    l,r=list(map(int,input().split()))
    L=max(L,l)
    R=min(R,r)
print((0 if R<L else R-L+1))
# Original solution
N,M = list(map(int,input().split()))
LR = [list(map(int,input().split())) for i in range(M)]

arr = [0 for i in range(M)]
seki = set(range(LR[0][0],LR[0][1]+1))
for i in range(M):
    arr[i] = set(range(LR[i][0],LR[i][1]+1))
    seki = seki & arr[i]
print((len(seki)))


# Optimized solution
N,M = list(map(int,input().split()))
LR = [list(map(int,input().split())) for i in range(M)]

seki = set(range(LR[0][0],LR[0][1]+1))
for i in range(M):
    seki = seki & set(range(LR[i][0],LR[i][1]+1))
print((len(seki)))
# Original solution
N,M= list(map(int, input().split()))
li=[list(map(int,input().split())) for _ in range(M)]
id = [0]*N
for i in li:
    for j in range(i[0]-1,i[1]):
        id[j]+=1
print((len([i for i,x in enumerate(id) if x==M])))

# Optimized solution
N,M= list(map(int, input().split()))
li=[list(map(int,input().split())) for _ in range(M)]
mi=0
ma=N
for i in li:
    mi=max(mi,i[0])
    ma=min(ma,i[1])
if ma>=mi:
    print((ma-mi+1))
else:
    print((0))
# Original solution
N, M =list(map(int, input().split()))
L = [0 for i in range(N+1)]
R = [0 for i in range(N+1)]
for i in range(M):
    l,r = list(map(int, input().split()))
    L[l] += 1
    R[r] += 1
cnt = 0
tmp = 0
for i in range(N+1):
    tmp += L[i]
    if tmp == M:
        cnt += 1
    tmp -= R[i]
print(cnt)


# Optimized solution
n,m = list(map(int, input().split()))
ll = 1
rr = n
for i in range(m):
    l,r = list(map(int,input().split()))
    if l > ll:
        ll = l
    if r < rr:
        rr = r
print((max(rr - ll + 1, 0)))
# Original solution
N, M =list(map(int, input().split()))
li = []
l, r = list(map(int, input().split()))
for j in range(r-l+1):
    li.append(j + l)
for i in range(M-1):
    l, r = list(map(int, input().split()))
    # for j in range(r-l+1):
    li = [s for s in li if l <= s and s <= r]
print((len(li)))

# Optimized solution
N, M =list(map(int, input().split()))

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

for i in range(M-1):
    l2, r2 = list(map(int, input().split()))
    if l2 > l:
        l = l2
    if r > r2:
        r = r2
if r - l + 1 > 0:
    print((r - l + 1))
else:
    print((0))
# Original solution
n,m = list(map(int,input().split()))
a = [k for k in range(1,10**5+1)]
s = set(a)
for i in range(m):
  l,r = list(map(int,input().split()))
  ml = [p for p in range(l,r+1)]
  newset = set(ml)
  s = s&newset
print((len(s)))

# Optimized solution
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)

ans = min(R)-max(L)+1
print((ans if ans >0 else "0"))
# Original solution

def solve():
#    dp = [1]*(N)
    dp = [0] * N
    for i in range(M):
#        for j in range(0, LR[i][0]-1):
#            dp[j] = 0
#        for j in range(LR[i][1], N):
#            dp[j] = 0
        for j in range(LR[i][0]-1,LR[i][1]):
            dp[j] += 1
#    print(dp)
    print((dp.count(M)))
#    print(sum(dp))


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


# Optimized solution

def solve():
    dp = [0] * N
    L = max(l)
    R = min(r)
    if L <= R:
        i = list(range(L,R+1))
        ans = len(i)
    else:
        ans = 0
    print(ans)

if __name__ == "__main__":
    N,M = list(map(int, input().split()))
#    LR = [list(map(int, input().split())) for _ in range(M)]
    l,r = [0]*M, [0]*M
    for c in range(M):
        tmp = list(map(int, input().split()))
        l[c] = tmp[0]
        r[c] = tmp[1]
    solve()
# Original solution
N,M=list(map(int,input().split()))
S=set()
for i in range(N):
  S.add(i+1)
for i in range(M):
  L,R=list(map(int,input().split()))
  T=set()
  for i in range(L,R+1):
    T.add(i)
  S=S&T
print((len(S)))


# Optimized solution
N,M=list(map(int,input().split()))
L=1
R=100000
for i in range(M):
  l,r=list(map(int,input().split()))
  L=max(L,l)
  R=min(r,R)
if L>R :
  print((0))
else :
  print((R-L+1))
# Original solution
N,M = list(map(int,input().split()))
L,R = list(map(int,input().split()))
a = []
for i in range(L,R+1):
    a.append(i)

for i in range(1,M):
    b = []
    l,r = list(map(int,input().split()))
    for j in range(l,r+1):
        b.append(j)
    a = list(set(a) & set(b))

print((len(a)))


# Optimized solution
N,M = list(map(int,input().split()))

l_max = 1
r_min = N
for i in range(0,M):
    l,r = list(map(int,input().split()))
    if l_max < l:
        l_max = l
    if r_min > r:
        r_min = r

if l_max > r_min:
    print((0))
else:
    print((r_min-l_max+1))
# Original solution
N, M = list(map(int, input().split()))
list1 = []
ss = set(x for x in range(1,N+1))
for i in range(M):
  ar = []
  L, R = list(map(int, input().split()))
  st = set(x for x in range(L, R+1))
  list1.append(st)
for s in list1:
  ss = ss & s
print((len(ss)))
    

# Optimized solution
N, M = list(map(int, input().split()))
ss = set(x for x in range(1,N+1))
for i in range(M):
  
  L, R = list(map(int, input().split()))
  st = set(x for x in range(L, R+1))
  ss = ss & st
print((len(ss)))
# Original solution
N, M = list(map(int, input().split()))
set1 = set([x for x in range(1, N+1)])
for i in range(M):
  L, R = list(map(int, input().split()))
  lr = set([x for x in range(L, R+1)])
  set1 =set1 & lr
print((len(set1)))

# Optimized solution
N, M = list(map(int, input().split()))
L, R = list(map(int, input().split()))


for i in range(M-1):
      L1, R1 = list(map(int, input().split()))
      if R1 < L or R < L1:
        print((0))
        exit()
      else:
        if L <= L1 <= R:
          L = L1
          
        elif L <= R1 <= R:
          R = R1
        
          
print((R-L+1))
# Original solution
n , m = list(map(int,input().split()))
p = {i+1 for i in range(n)}
for i in range(m):
    l , r = list(map(int,input().split()))
    s = {l+i for i in range(r-l+1)}
    p = p & s

print((len(p)))


# Optimized solution
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)

a = max(l)
b = min(r)
if a > b:
    print((0))
else:
    print((b-a+1))
# Original solution
n, m = list(map(int, input().split()))
a = []
for i in range(m):
    l, r = list(map(int, input().split()))
    if i == 0: a = set([int(i) for i in range(l, r+1)])
    else:
        a &= set([int(i) for i in range(l, r+1)])
print((len(a)))

# Optimized solution
n, m = list(map(int, input().split()))
a = []
flag = True
for i in range(m):
    l, r = list(map(int, input().split()))
    if i == 0: 
        l1 = l
        r1 = r
    else:
        if r < l1 or r1 < l:
            flag = False
        elif l1 < l:
            l1 = l
        elif r1 > r:
            r1 = r
if flag: print((r1-l1+1))
else: print((0))
# Original solution
# スペース区切りの整数の入力
N,M = list(map(int, input().split()))
LR_list=[]
Card_list=[]

#LR読み込み
for i in range(M):
  LR_list.append([int(e) for e in input().split()])

#初期化
for i in range(N):
  Card_list.append(0)

#LRをCardListに適用
for i in range(M):
  #print("")
  for j in range(LR_list[i][0],LR_list[i][1]+1,1):
    Card_list[j-1]+=1
    #print(j,end="")
    
ans=0
    
for i in range(N):
  if Card_list[i]==M:
    ans+=1
    
print(ans)

# Optimized solution
# スペース区切りの整数の入力
N,M = list(map(int, input().split()))
LR_list=[]
Card_list=[]

Min_L = 1
Max_R = N

#LR読み込み
for i in range(M):
  L,R = list(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))
# Original solution
N,M = list(map(int, input().split()))
LR = [list(map(int,input().split())) for i in range(M)]

passable = [1]*(N+1)

for l,r in LR:
    for i in range(l):
        passable[i]=0
    for i in range(r+1, N+1):
        passable[i]=0

print((passable.count(1)))

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

for i in range(M):
    L[i],R[i] = list(map(int, input().split()))
    
l = max(L)
r = min(R)

print((max(r-l+1,0)))
# Original solution
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return eval(input())

def main():
    N, M = LI()
    LR = []
    for _ in range(M):
        LR.append(LI())
    SE = None
    for l, r in LR:
        se = set(range(l, r + 1))
        if SE is None:
            SE = se
        else:
            SE = SE & se
    ans = len(SE)
    return ans

print((main()))


# Optimized solution
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return eval(input())

def main():
    N, M = LI()
    LR = []
    for _ in range(M):
        LR.append(LI())
    left = 0
    right = INF
    for l, r in LR:
        left = max(left, l)
        right = min(right, r)
    ans = max(right - left + 1, 0)
    return ans

print((main()))
# Original solution
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline

def main():
    gates={}
    s={}
    n,m = list(map(int,input().split()))
    gates=[list(map(int,input().split())) for _ in range(m)]

    for gate in gates:
        if len(s)==0:
            s = set([i for i in range(gate[0],gate[1]+1)])
        else:
            s = s & set([i for i in range(gate[0],gate[1]+1)])
    print((len(s)))

if __name__=="__main__":
    main()

# Optimized solution
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline

def main():
    gates=[]
    n,m = list(map(int,input().split()))
    gates=[list(map(int,input().split())) for _ in range(m)]
    min_data=0
    max_data=10**5+1

    for gate in gates:
        if min_data < gate[0]:
            min_data=min(gate)
        if max_data > gate[1]:
            max_data=max(gate)
    print((len(list(range(min_data,max_data+1)))))

if __name__=="__main__":
    main()
# Original solution
n,m = list(map(int,input().split()))
l = []
r = []
low = 0
high = 10 ** 5 + 1
for i in range(m):
    ll,rr = list(map(int,input().split()))
    l.append(ll)
    r.append(rr)
    low = max(low,ll)
    high = min(high,rr)

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


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

low = 0
high = 10 ** 5 + 1
for i in range(m):
    ll,rr = list(map(int,input().split()))
    
    low = max(low,ll)
    high = min(high,rr)

if high>=low:
    print((high-low+1))
else:
    print((0))
# Original solution
data=input().split()
N=int(data[0])
M=int(data[1])
data_gate=[input().split() for i in range(M)]
ans=0
for i in range(1,N+1):
    for j in range(M):
        minimum=int(data_gate[j][0])
        maximum=int(data_gate[j][1])
        if j<=M-2:
            if minimum<=i<=maximum:
                continue
            else:
                break
        else:
            if minimum<=i<=maximum:
                ans+=1
            else:
                break
print(ans)

# Optimized solution
data=input().split()
N=int(data[0])
M=int(data[1])
data_gate=[input().split() for i in range(M)]
data_min=[]
data_max=[]
for i in range(M):
    data_min.append(int(data_gate[i][0]))
    data_max.append(int(data_gate[i][1]))
m=max(data_min)
M=min(data_max)
if M>=m:
    ans=M-m+1
else:
    ans=0
print(ans)
# Original solution
data=input().split()
N=int(data[0])
M=int(data[1])
data_gate=[input().split() for i in range(M)]
data_min=[]
data_max=[]
for i in range(M):
    data_min.append(int(data_gate[i][0]))
    data_max.append(int(data_gate[i][1]))
m=max(data_min)
M=min(data_max)
if M>=m:
    ans=M-m+1
else:
    ans=0
print(ans)

# Optimized solution
r=input().split()
N=int(r[0])
M=int(r[1])
data=[[int(s) for s in input().split()] for i in range(M)]
left=[]
right=[]
for i in range(M):
    left.append(data[i][0])
    right.append(data[i][1])
if min(right)-max(left)+1<=0:
    print((0))
else:
    print((min(right)-max(left)+1))
# Original solution
n, m = list(map(int, input().split()))
cnt = [0]*(n+1)
for i in range(m):
  l, r = list(map(int, input().split()))
  for j in range(l, r+1):
    cnt[j] += 1
print((cnt.count(m)))    


# Optimized solution
n, m = list(map(int, input().split()))
l = [0]*m
r = [0]*m
for i in range(m):
  l[i], r[i] = list(map(int, input().split()))
print((max(min(r) - max(l) + 1, 0)))
# Original solution
import sys
input = sys.stdin.readline
N, M = list(map(int, input().split()))
ans = 0
card = [1] * N
for _ in range(M):
	L, R = list(map(int, input().split()))
	for i in range(N):
		if i < L-1 or R-1 < i:
			card[i] = 0

print((card.count(1)))

# Optimized solution
import sys
input = sys.stdin.readline
N, M = list(map(int, input().split()))
max_L = 1
min_R = 100000
for _ in range(M):
	L, R = list(map(int, input().split()))
	if max_L < L:
		max_L = L
	if R < min_R:
		min_R = R
if min_R - max_L + 1 < 0:
	print((0))
else:
	print((min_R - max_L + 1))
# Original solution
n, m = list(map(int, input().split()))
l = [0 for _ in range(n)]

for _ in range(m):
  a, b = [int(x)-1 for x in input().split()]
  for i in range(b-a+1):
    l[a+i] += 1
  
print((l.count(m)))

# Optimized solution
n, m = list(map(int, input().split()))
l = []
r = []

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

if l_max <= r_min:
  print((r_min - l_max + 1))
else:
  print((0))
# Original solution
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
''' 
------------------------
author : iiou16
------------------------
'''

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

    L = []
    R = []
    id = list(range(N+1))
    id_set = set(id)
    for _ in range(M):
        if len(id) == 0:
            break
        l, r = list(map(int, input().split()))
        id_set = id_set & set(id[l:r+1])
    print((len(id_set)))

if __name__ == '__main__':
    main()

# Optimized solution
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
''' 
------------------------
author : iiou16
------------------------
'''

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

    L = []
    R = []
    id = list(range(N+1))
    for _ in range(M):
        l, r = list(map(int, input().split()))
        L.append(l)
        R.append(r)

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

if __name__ == '__main__':
    main()
# Original solution
N,M = (int(i) for i in input().split())
e = [[int(i) for i in input().split()] for i in range(M)]
hoge = set()

zenbu = {int(i + 1) for i in range(N)}

for i in e:
    konkaino = {int(i) for i in range(i[0],i[1] + 1)}
    zenbu = zenbu & konkaino


print((len(zenbu)))

# Optimized solution
N,M = (int(i) for i in input().split())
e = [[int(i) for i in input().split()] for i in range(M)]

right = N
left = 0

for i in e:
    if left < i[0]:
        left = i[0]
    if right > i[1]:
        right = i[1]
if right >= left:
    print((right - left + 1))
else:
    print("0")
# Original solution
N, M = list(map(int, input().split()))
dict = {}
for _ in range(M):
    L, R = list(map(int, input().split()))
    for x in range(L, R+1):
        if x in dict:
            dict[x] += 1
        else:
            dict[x] = 1
print((sum([1 for k, v in list(dict.items()) if v == M])))

# Optimized solution
n, m = list(map(int, input().split()))
L, R = 1, n
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)))
# Original solution
nm = list(map(int, input().split()))
n = nm[0]
m = nm[1]

gates = [list([int(x) for x in input().split()]) for i in range(m)]

keys = [i+1 for i in range(n)]
key_count = [0 for i in range(n)]

for gate in gates:
  new_key = keys.copy()

  for key in keys:
    if (key >= gate[0] and key <= gate[1]):
      key_count[key-1] += 1
    else:
      new_key.remove(key)
  keys = new_key

print((len(keys)))

# Optimized solution
nm = list(map(int, input().split()))
n = nm[0]
m = nm[1]

gates = [list([int(x) for x in input().split()]) for i in range(m)]

first_max = 0
second_min = 100001
for gate in gates:
  first_max = max(first_max, gate[0])
  second_min = min(second_min, gate[1])

answer = second_min - first_max + 1
if (answer <= 0):
  print((0))
else:
  print(answer)
# Original solution
N,M=list(map(int,input().split()))

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

IDlist=[i for i in range(N)]

for l,r in LR:
    LRlist=[i for i in range(l-1,r)]
    IDlist=set(IDlist)&set(LRlist)
print((len(IDlist)))

# Optimized solution
N,M=list(map(int,input().split()))

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

L=0
R=N
for l,r in LR:
    if L<l:
        L=l
    if R>r:
        R=r
    #print(str(L)+str(R))

if R-L+1<0:
    print((0))
else:
    
    print((str(R-L+1)))
# Original solution
N, M = list(map(int, input().split()))

sub = set(i for i in range(1, N + 1))
for _ in range(M):
    L, R = list(map(int, input().split()))
    sub &= set(i for i in range(L, R + 1))
print((len(sub)))

# Optimized solution
N, M = list(map(int, input().split()))

st = 1
en = N
for _ in range(M):
    L, R = list(map(int, input().split()))
    st = max(st, L)
    en = min(en, R)

ans = en - st
if ans < 0:
    print((0))
else:
    print((ans + 1))