peterverebics/CodeNet_Python_118 huggingface dataset:
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(1 - max(L) + min(R) if max(L) < min(R) else 0)
n, m = map(int, input().split())

left = 0
right = float('inf')
for i in range(m):
    l, r = map(int, input().split())
    left = max(left, l)
    right = min(right, r)
    
print(right - left + 1)
n,m = map(int,input().split())
lr = [list(map(int,input().split())) for _ in range(m)]

l = 0
r = n-1

for l1,r1 in lr:
    l = max(l, l1)
    r = min(r, r1)

print(max(0, r - l + 1))
N,M=map(int,input().split())
LR=[list(map(int, input().split())) for _ in range(M)]
left=0
right=N+1
ans=0
for i in range(M):
	left=max(left,LR[i][0])
	right=min(right,LR[i][1])
ans=right-left+1
print(ans)
import sys
input = lambda: sys.stdin.readline().rstrip()

def main():
    n, m = map(int, input().split())
    L, R = 0, n+1
    for _ in range(m):
        l, m = map(int, input().split())
        if l > L: L = l
        if m < R: R = m
    print(R-L+1)
    
if __name__ == '__main__':
    main()
N,M = map(int, input().split())
Key = [0 for i in range(N+1)]
for _ in range(M):
    L,R = map(int,input().split())
    L -= 1
    R -= 1
    Key[L] += 1
    Key[R+1] -= 1
count = 0
for i in range(len(Key)):
    if i > 0:
        Key[i] += Key[i-1]
        if Key[i] == M:
          count += 1
print(count)
n,m=list(map(int, input().split()))
l = 0
r = float("inf")
for _ in range(m):
    a,b = list(map(int, input().split()))
    l = max(l,a)
    r = min(r,b)

print(min(n, r-l+1))
N, M = map(int, input().split())

l_max = 1
r_min = N
for _ 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:
    print(0)
else:
    print(r_min-l_max+1)
#import math
#import itertools
import numpy as np
#from collections import deque

INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))

def do():
    n,m=INTM()
    ls=[]
    rs=[]
    for i in range(m):
        l,r=INTM()
        ls.append(l)
        rs.append(r)
    
    print(min(rs)-max(ls)+1)



    
if __name__=='__main__':
    do()
N,M = map(int, input().split())
Key = [0 for i in range(N+1)]
for _ in range(M):
    L,R = map(int,input().split())
    L -= 1
    R -= 1
    Key[L] += 1
    Key[R+1] -= 1
for i in range(len(Key)):
    if i > 0:
        Key[i] += Key[i-1]
for i in range(len(Key)-1):
    if i > 0:
        if Key[i-1] > 0 and Key[i] == 0 and Key[i+1] > 0:
            print(0)
            exit()
print(Key.count(max(Key)))
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-1):
    a = max(a,ls[j+1][0])
    b = min(b,ls[j+1][1])
    if b-a<0:
        print(0)
        sys.exit()
print(b-a+1)
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(min(Rlist)-max(Llist)+1)
N,M=map(int,input().split())
l=list()
r=list()
for n in range(M):
  L,R=map(int,input().split())
  l.append(L)
  r.append(R)
if min(r)>max(l):
  print(min(r)-max(l)+1)
else:
  print('0')
N, M = map(int, input().split())
L = list(list(map(int,input().split())) for i in range(M))
l = 1
r = N
for i in range(M):
    l = max(l,L[i][0])
    r = min(r,L[i][1])
print(min(r-l+1,0))
def resolve():
    #n=int(input())
    #a,b=map(int,input().split())
    #x=list(map(int,input().split()))
    #a=[list(map(lambda x:int(x)%2,input().split())) for _ in range(h)]
    n,m=map(int,input().split())
    lr=sorted([list(map(int,input().split())) for _ in range(m)])
    mr=10**6
    for i in range(m):
        mr=min(mr,lr[i][1])
    print(max(0,mr-lr[0][0]))

if __name__ == '__main__':
    resolve()
N, M = map(int, input().split())
LR = [list(map(int,input().split())) for i in range(M)]
count = 0

for i in range(N):
  for j in range(M):
    if LR[j][0] <= i <= LR[j][1]:
      count += 1

print(count)
#n=int(input())
#s,t=input().split()
n,m = map(int, input().split())
#l=list(map(int, input().split()))
l=[0]*100005
r=[1000005]*100005
z=1000010
for i in range(m):
   l[i],r[i]=map(int,input().split()) 
mi=max(l)
ma=min(r)




print(ma-mi+1)
import numpy as np

i = list(map(int, input().split()))
N = i[0]
M = i[1]

l = np.array([list(map(int, input().split())) for i in range(M)])

limit1 = max(l[:, 0])
limit2 = min(l[:, 1])

print(limit2 - limit1 + 1)
N,M = map(int,input().split())
a = list()
b = list()
for i in range(M):
  tmp_a,tmp_b =  map(int, input().split())
  a.append(tmp_a)
  b.append(tmp_b)
print(min(b)-max(a)+1 if min(b)-max(a) > 0 else 0)
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 a[0] < a[i+1]:
        ans[0] = a[i+1]
    if b[0] > b[i+1]:
        ans[1] = b[i+1]

print(ans[1]-ans[0]+1)
N,M = (int(x) for x in input().split())
R = N
L = 1
for i in range(M):
  l,r = (int(x) for x in input().split())
  if l > L:
    L = l
  if r < R:
    R = r
print(R-L+1)
n,m  = map(int, input().split())
l = []
c = []
for i in range(m):
    a,b = map(int, input().split())
    for j in range(a,b+1):
        l.append(j)
    l = sorted(l)
#print(l)
x = 0
y = 0
for k in range(len(l)-1):
    if l[k] ==l[k+1]:
        x+=1
        if x == m-1:
            y += 1
    else:
        x = 0
        continue

if y == 0:
    print(1)
else:
    print(y)
N,M = map(int,input().split())
L = [i for i in range(1,N+1)]
a,b = map(int,input().split())
for i in range(M-1):
    c,d = map(int,input().split())
    a = max(a,c)
    b = min(b,d)
print(b-a+1)
def main():
    N, M = map(int, input().split(" "))
    left = 0
    right = 10**5
    for i in range(M):
        a, b = map(int, input().split(" "))
        left = max(left, a)
        right = min(right, b)
    if not left == 1:
        print(right - left + 1)
    else:
        print(right)


if __name__ == '__main__':
    main()
N,M = map(int,input().split())
right = 10**5
left = 0
for _ in range(M):
  a,b = map(int,input().split())
  right = min(right,b) 
  left = max(left,a)

print(right-left+1)
def solve():
    N, M = map(int, input().split())
    
    c = 0
    l = 0
    r = 10 ** 9
    for _ in range(M):
        a, b = list(map(int, input().split()))
        l = max(l, a)
        r = min(r, b)
    
    if N < l:
        return 0
    elif N == l:
        return 1
    elif l < N < r:
        return N-l+1
    else:
        return r-l+1

print(solve())
N, M = [int(n) for n in input().split()]
L, R = (1, N)
for i in range(M):
    l, r = [int(n) for n in input().split()]
    L, R = (max(l, L), min(r, R))
print(R-L+1)
N, M = input().split(' ')
N = int(N)
M = int(M)
min = 1
max = N
for i in range(M):
  l, m = input().split(' ')
  l = int(l)
  m = int(m)
  if min>m or max<l:
    print(0)
    break 
  if min < l:
    min = l
  if max > m:
    max = m
print(max-min+1)
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
L = 0; R = 10 ** 6
for _ in range(M):
    l, r = lr()
    if l > L:
        L = l
    if r < R:
        R = r

answer = R - L + 1
print(answer)
N,M = map(int,input().split())
L = []
R = []
for _ in range(M):
    a,b = map(int,input().split())
    L.append(a)
    R.append(b)
Ls = set(range(L[0],R[0]+1))
Rs = set(range(L[-1],R[-1]+1))

print(len(Ls & Rs))
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(R-L+1)
n,m = map(int, input().split())

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

print(b[0]-a[m-1]+1)
N,M = map(int, input().split())
ansL = 0
ansR = 0
for i in range(M):
    L,R = map(int,input().split())
    if M-1 == 0:
      ansL = L
      ansR = R
      break
    if i == 0:
        ansR = R
    elif i == M-1:
        ansL = L
print(ansR-ansL+1)
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(l-r+1,0))
n,m = map(int,input().split())
L = [0]*m
R = [0]*m

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)
print(min_r-max_l+1)
N, M = map(int, input().split())
L, R = [], []
for i in range(M):
  tmp = list(map(int, input().split()))
  L.append(tmp[0])
  R.append(tmp[1])

lower = max(L)
upper = min(R)
print(upper-lower+1)
N,M = map(int,input().split())
k_1 = []
k_2 = []
for i in range(M):
  l,r = map(int,input().split())
  k_1.append(l)
  k_2.append(r)
if min(k_2)>N:
  S = N
else:
  S = min(k_2)
print(S-max(k_1)+1)
N,M=map(int,input().split())
ans=0
L,R=0,10**5
for i in range(M):
    l,r=map(int,input().split())
    L=max(L,l); R=min(R,r)
print(L,R)
R=min(R,N)
print(abs((L-1)-R))
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(a-b+1)
resolve()
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 = min(rs) - max(ls) + 1
print(s)
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(min(r-l+1,0))
n,m=map(int,input().split())
lh=0
rh=10**9
for i in range(m):
  l,r=map(int,input().split())
  if lh<=l:
    lh=l
  if rh>=r:
    rh=r
print(rh-lh+1)
# coding: utf-8
import sys
import itertools


def sr(): return sys.stdin.readline().rstrip()


def ir(): return int(sr())


def lr(): return list(map(int, sr().split()))


N, M = lr()
l_list = []
r_list = []
for i in range(M):
    l, r = lr()
    l_list.append(l)
    r_list.append(r)

maxL = 0
minR = 100000000
for i in range(M):
    if maxL == 0:
        maxL = l_list[i]
        minR = r_list[i]
        continue
    max(maxL, l_list[i])
    min(minR, r_list[i])
ans = max(maxL - minR + 1, 0)
n, m = map(int, input().split())

s = [0] * (n+2)

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

ans = [0] * (n+2)
for i in range(1, n+2):
  ans[i] = ans[i-1] + s[i]

print(ans.count(max(ans)))
# import sys
# sys.setrecursionlimit(10 ** 6)
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
    print(minr - maxl + 1)


if __name__ == '__main__':
    solve()
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 11 01:27:46 2020

@author: liang
"""

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

ans = min(R) - max(L) + 1
print(ans)
N,M = map(int, input().split())
li = [list(map(int, input().split())) for i in range(M)]

count = 0

mi = 0
ma = 10**9

for index, n in enumerate(li):
  if mi < n[0]:
    mi = n[0]

  if ma > n[1]:
    ma = n[1]

ans = N - (mi - 1) - (N - ma)

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

mm=0
mx=10**5
for _ in range(M):
    l,r = map(int,input().split())
    mm = max(mm,l)
    mx= min(mx,r)
print(mx-mm+1)
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_b-min_a+1)
#!/usr/bin/env python3
import sys
def input():
    return sys.stdin.readline()[:-1]

def main():
    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())

    ans = min(R) - max(L) + 1
    print(ans)

if __name__ == '__main__':
    main()
def resolve():
    N, M = map(int, input().split())

    start_index = 1
    last_index = N
    for _ in range(M):
        pp, ss = map(int, input().split())
        start_index = max(start_index, pp)
        last_index = min(last_index, ss)

    print(last_index - start_index + 1)


resolve()
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])
    print(a)
    print(b)
    if b-a<0:
        print(0)
        sys.exit()
print(b-a+1)
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])
print(min_r-max_l+1)
import numpy as np
def solve():
    N,M = map(int, input().split())
    LR = np.array([list(map(int, input().split())) for _ in range(M)])
    Lmax = np.max(LR[:,0])
    Rmin = np.min(LR[:,1])
    print(Rmin - Lmax + 1)





if __name__ == '__main__':
    solve()
n,m = map(int, raw_input().split(' '))
def intersection(A,B):
	a,b  = max(A[0],B[0]), min(A[1],B[1])
	return [a,b] if a <= b else []

r = [1,n+1]
for _ in range(m):
	a,b = map(int, raw_input().split(' '))
	keys = intersection(r, [a,b])
	if len(keys) == 0: break
print (keys[1] - keys[0] + 1) if keys else 0
N, M = map(int, input().split())
l, r = 1, 10 ** 5
for _ in range(M):
    li, ri = map(int, input().split())
    l = max(l, li)
    r = min(r, ri)
print(r - l + 1)
# -*- coding: utf-8 -*-
def main():
    N,M = map(int,input().split())
    LR = [list(map(int,input().split())) for i in range(M)]
    LR.sort(key=lambda x: x[0],reverse=True)
    lmax = LR[0][0]
    LR.sort(key=lambda x: x[1])
    rmin = LR[0][1]
    print(rmin-lmax+1)
if __name__ == '__main__':
    main()
n,m=map(int, input().split())
l_ans=1
r_ans=n
for i in range(m):
    l,r=map(int, input().split())
    l_ans=max(l_ans,l)
    r_ans=min(r_ans,r)
if r>=l:
    print(r_ans-l_ans+1)
else:
    print(0)
n, m = map(int, input().split())
arr = []
for i in range(m):
  l, r = map(int, input().split())
  arr.append([l, l + 1, r])
ans = 0
for i in range(n):
  f = 0
  for j in range(m): 
    if i + 1 in arr[j]:
      next
    else:
      f = 1
      break
  if f == 0:
    ans += 1
if m == 1:
  ans = n
print(ans)
n,m=map(int,input().split())
low=1
high=n
for i in range(m):
  l,r=map(int,input().split())
  if l>low:
    low=l
  if r<high:
    high=r
  print(low,high)
if high>=low:
  print(high-low+1)
else:
  print(0)
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
    
print(r-l+1)
N, M = map(int, input().split())

id_range = range(N + 1)

for m in range(M):
  L, R = map(int, input().split())
  if L not in id_range and R not in id_range:
    id_range = range(0)
    break
  elif L in id_range:
    id_range = range(L, id_range[len(id_range) - 1] + 1, 1)
    if R in id_range:
      id_range = range(id_range[0], R + 1, 1)
    
id_set = set(id_range)
print(len(set(range(1, N + 1, 1)) & id_set))
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(min(min(r) - max(l)+1, n))
n,m=map(int,input().split())
low=1
high=n 
for _ in range(m):
    l,r=map(int,input().split())
    low=max(l,low)
    high=min(r,high)
print(high-low+1)
n, m = map(int, input().strip().split())

max_left = 0
min_right = n
for _ in range(m):
    l_tmp, r_tmp = map(int, input().strip().split())
    if max_left < l_tmp:
        max_left = l_tmp
    if min_right > r_tmp:
        min_right = r_tmp

print(min_right - max_left + 1)
N,M=map(int,input().split())
temp=0
TEMP=N-1
for i in range(M):
    a,b=map(int,input().split())
    if not temp<=a-1<=TEMP and not temp<=b-1<=TEMP:
        print(0)
        exit()
    if a-1>temp:
        temp=a-1
    if TEMP>b-1:
        TEMP=b-1
    
print(TEMP-temp+1)
n, m = map(int, input().split())
a = [list(map(int, input().split())) for i in range(m)]

ans = 0

l = []
r = []

for i in range(m):
  l.append(a[i][0])
  r.append(a[i][1])
    

print(min(r)-max(l)+1)
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)
print(min_r - max_l + 1)
import sys
def input():
    return sys.stdin.readline()
def main():
    N, M = map(int, input().split())
    dict = {}
    for _ in range(M):
        L, R = map(int, input().split())
        for x in range(L, R+1):
            if x in dict:
                dict[x] += 1
            else:
                dict[x] = 1
    return sum([1 for k, v in dict.items() if v == M])
main()
#abc127 c
n,m=map(int,input().split())
l_max=0
r_mini=n
for i in range(m):
    l,r=map(int,input().split())
    if l>l_max:
        l_max=l
    if r_mini>r:
        r_mini=r
ans=r_mini-l_max+1
print(ans)
n,m=map(int,input().split())
l=0
r=10**5+1
for _ in range(m):
  l2,r2=map(int,input().split())
  l=max(l,l2)
  r=min(r,r2)
print(r-l+1)
N, M = [int(i) for i in input().split()]
left = 0
right = N - 1
for _ in range(M):
  L, R = [int(i) for i in input().split()]
  if left < L:
    left = L
  if right > R:
    right = R
print(right-left+1)
n, m = map(int, input().split())
l, r = map(int, input().split())
A = [l, r]
for i in range(1, m):
  l, r = map(int, input().split())
  if A[0] < l:
    A[0] = l
  if A[1] > r:
    A[1] = r
print(r-l+1)
N, M = map(int, input().split())
gate = [None] * M
range_L = 1
range_R = N
for i in range(M):
    gate[i] = tuple(map(int, input().split()))
    left = gate[i][0]
    right = gate[i][1]
    if range_L < left:
        range_L = left
    if right < range_R:
        range_R = right

ans = range_R - range_L + 1
if ans < 0:
    print(ans)
else:
    print(ans)
n, m = map(int, input().split())

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

print(min(r) - max(l) + 1)
n, m = map(int, input().split())
start = 1
end = n
for i in range(m):
    l, r = map(int, input().split())
    if start < l:
        start = l
    if r < end:
        end = r
print(end - start + 1)
n, m = map(int, input().split())

L = 1
R = 10**5

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

print(R - L + 1)
card_num, gate_num = map(int, input().split())
table = []
count = 0

for i in range(gate_num):
    start_num, end_num = map(int, input().split())
    number = end_num - start_num
    table.append(number)

list.sort(table)
if gate_num == 1:
    print(card_num)
elif table[gate_num-1] == table[0]:
    print(table[0])
else:
    print(table[gate_num-1] - table[0])
import sys
import os


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, M = list(map(int, sys.stdin.buffer.readline().split()))
    LR = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]
    mi = 0
    mx = 10 ** 5 + 1

    for lr in LR:
        l = lr[0]
        r = lr[1]
        mi = max(mi, l)
        mx = min(mx, r)

    print(mx - mi + 1)


if __name__ == '__main__':
    main()
n, m = map(int, input().split())
cnt = [0] * (n + 3)
cnt_sum = [0] * (n + 2)
for _ in range(m):
    l, r = map(int, input().split())
    cnt[l] += 1
    cnt[r + 1] -= 1
for i in range(1, n + 2):
    cnt_sum[i] = cnt_sum[i - 1] + cnt[i]
print(cnt_sum.count(max(cnt_sum)))
s = input().split()
n=int(s[0])
m=int(s[1])
l=0
r=float('inf')
for i in range(m):
    s = list(map(int,input().split()))
    l=max(l,s[0])
    r=min(r,s[1])
print(r-l+1)
def mapt(fn, *args):
    return tuple(map(fn, *args))


def Input():
    return mapt(int, input().split(" "))


def main():
    n, m = Input()
    max_l = 0
    min_r = 1e6
    for i in range(m):
        l, r = Input()
        max_l = max(max_l, l)
        min_r = min(min_r, r)
    print((min_r+1) - max_l)

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)
print(min(R)-min(L)+1)
n, m = map(int, input().split())
l = 0
r = n
for i in range(m):
    lt, rt = map(int, input().split())
    if (lt > l):
        l = lt
    if (rt < r):
        r = rt
if (r <l):
    print(l,r)
    print(0)
else:
    print(r-l+1)
n,m = map(int, input().split())
max_l = float("-inf")
min_r = float("inf")
for _ in range(m):
  l,r = map(int, input().split())
  max_l = max(max_l, l)
  min_r = min(min_r, r)
if l <= r:
  print(min_r - max_l + 1)
else:
  print(0)
n, m = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(m)]

max_L = 0
min_R = float("inf")
for i in range(m):
    L, R = LR[i]
    max_L = max(max_L, L)
    min_R = min(min_R, R)
print(min_R - max_L + 1)
def main():
  IN= open(0).read().split()[2:];L,R=map(int,[max(IN[::2]),min(IN[1::2])]);print(R-L+1 if R-L>0 else 0)
if __name__=="__main__":
  main()
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 = max(l)
r_min = min(r)

print(r_min-l_max+1)
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())
  
count = 0

for i in range(N):
  if max(L) <= i <= min(R):
    count += 1

print(count)
import sys
N,M=map(int, sys.stdin.readline().split())
ll=float("-inf")
rr=float("inf")
for _ in range(M):
    l,r=map(int, sys.stdin.readline().split())
    ll=max(ll,l)
    rr=min(rr,r)

print rr-ll+1
n, m = map(int, input().split())
gl = []
for i in range(m):
  g = list(map(int, input().split()))
  gl.append(g)
for j in range(1, m):
  db = gl[j][0] - gl[j - 1][0]
  da = gl[j][1] - gl[j - 1][1]
  if db >= 0:
    if da >= 0:
      gl[j][1] = gl[j - 1][1]
  else:
    if da >= 0:
      gl[j][0] = gl[j - 1][0]
      gl[j][1] = gl[j - 1][1]
    else:
      gl[j][0] = gl[j - 1][0]
print(gl[-1][1] - gl[-1][0] + 1)
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)
n,m = map(int,input().split())
#v = list(map(int,input().split()))

#a = [0 for _ in range(n+1)]
ml = -1
mr = 10**6
for i in range(m):
  l,r = map(int,input().split())
  ml = max(ml,l)
  mr = min(mr,r)

res = mr-ml + 1
print(res)
N,M= list(map(int,input().split()))
end = []
start = []
for i in range(M):
    A,B = list(map(int,input().split()))
    end.append(B)
    start.append(A)
print(min(end) - max(start)+1)
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)
x=max(l)
y=min(r)
ans=(y-x+1)
print(ans)
N, M = map(int, input().split())
left, right = map(int, input().split())
for _ in range(M-1):
    c_left, c_right = map(int, input().split())
    if c_left > left:
        left = c_left
    if c_right < right:
        right = c_right

print(right-left+1)
n,m = map(int,input().split())
mi = 1
ma = 100000
for i in range(m):
    l,r = map(int,input().split())
    if l > mi:
        mi = l
    if r < ma:
        ma = r
print(ma-mi + 1)
n, m = map(int, input().split())
l = 1
r = 10**5
for _ in range(m):
  l_, r_ = map(int, input().split())
  l = max(l, l_)
  r = min(r, r_)
  
print(r-l+1)
N,M=map(int,input().split())
mn=0
mx=N
for i in range(M):
 L,R=map(int,input().split())
 mn=max(L,mn)
 mx=min(R,mx)
print(mx-mn+1)
n, m = map(int, input().split())
lr_list = [list(map(int, input().split())) for _ in range(m)]
left = 0
right = 10 ** 10
for l, r in lr_list:
    left = max(left, l)
    right = min(right, r)
print(right - left + 1)
N, M = map(int, input().split())

cards = list(range(1, N+1))
for _ in range(M):
    l, r = map(int, input().split())
    if l > cards[-1] or r < cards[0]:
        continue
    cards = list(set(cards) & set(range(l, r+1)))
print(len(cards))
N, M = map(int,input().split())
L = []
R = []
for i in range(M):
    l, r = input().split()
    L.append(int(l))
    R.append(int(r))

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