peterverebics/CodeNet_Python_118 huggingface dataset:
N,M = map(int,input().split())
mi = 0
ma = 10 ** 6
for i in range(M):
  L,R = map(int,input().split())
  mi = max(mi,L)
  ma = min(ma,R)
print(str(ma - mi + 1))
_, __, *d = map(int, open(0).read().split())
print(min(d[1::2]) - max(d[::2]) + 1)
n,m = map(int,input().split(' '))
l = []
start = 0
end = n
count = 0
f = False

for i in range(m):
    l.append(list(map(int,input().split(' '))))

for i in l:
    if i[0] >= start:
        start = i[0]
    if i[1] <= end:
        end = i[1]

print(end-start+1)
N,M = map(int,input().split())
L = 0
R = N
for m in range(M):
    L_new,R_new = map(int,input().split())
    if L_new > L:
        L = L_new
    if R_new < R:
        R = R_new

if R - L <= 0:
    print(0)
else:
    print(R-L+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)
R=min(R,N)
print(abs((L-1)-R))
n, m = map(int, input().split())

card = list(range(n))

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

print(len(card))
n,m = map(int,input().split())
i_now = 0
r_now = 10**5 + 1

for k in range(m):
  i,r = map(int,input().split())
  if i_now < i:
    i_now = i

  if r_now > r:
    r_now = r
    
if n < r:
  r_now = n
  
ans = r_now - i_now + 1
  
print(ans)
N, M = map(int, input().split())
LL = 0
RR = N
for i in range(M):
    L, R = map(int, input().split())
    if LL < L:
        LL = L
    if RR > R:
        RR = R
print(RR - LL + 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())
if max(l)<min(r):
    print(0)
elif max(l)>min(r):
    print(0)
else:
    print(min(r)-max(l)+1)
n, m = map(int, input().split())
a = []
flag = True
for i in range(m):
    l, r = map(int, input().split())
    if i == 0: 
        l1 = l
        r1 = r
    else:
        if r < l1 or r1 < l:
            flag = False
            exit()
        elif l1 < l:
            l1 = l
        elif r1 > r:
            r1 = r
if flag: print(r1-l1+1)
else: print(0)
N, M = [int(s) for s in input().split(' ')]
l = 1
r = N
for m in range(M):
    L, R = [int(s) for s in input().split(' ')]
    l = max(l, L)
    r = min(r, R)
print(r - l + 1)
N, M= map(int, input().split())

L,R = 0,100100
ans = [0]*(N+1)
for i in range(M):
    a,b = map(int,input().split())
    L = max(L,a)
    R = min(R,b)
print(R-L+1)
N,M= list(map(int,input().split()))
list_A = [0] * N
for i in range(M):
    A,B = list(map(int,input().split()))
    list_A[A:B+1] = map(lambda x: x+1,list_A[A:B+1])
print(list_A.count(M))
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])

print(min(R) - max(L) + 1)
N,M=map(int, input().split())
max_L = 1
min_R = N
#print(max_L,min_R)
for i in range(M):
  L,R=map(int, input().split())
  max_L = max([max_L,L])
  min_R = min([min_R,R])
#  print(max_L,min_R)

print(min_R-max_L+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())

ans = []
for i in range(M):
    ans.extend(L[i:])
    ans.extend(R[:i+1])

import numpy as np

splited = np.array_split(ans, M)

S = set(i for i in range(1, N+1))
for s in splited:
    S = S & set(s)

print(len(S))
# import math
# import statistics
# a=input()
#b,c=int(input()),int(input())
# c=[]
# for i in a:
#     c.append(i)
e1,e2 = map(int,input().split())
#f = list(map(int,input().split()))
#g = [input() for _ in range(a)]
h = []
for i in range(e2):
    h.append(list(map(int,input().split())))
L=[]
R=[]
for i in range(len(h)):
    L.append(h[i][0])
for i in range(len(h)):
    R.append(h[i][1])

print(min(R)-max(L)+1)
n,m = map(int,input().split())

ans,ansf = 1,n

for i in range(m):
    l,r = map(int,input().split())
    ans = max(ans,l)
    ansf = min(ansf,r)
    
print(ansf-ans+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())

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

print(R - L + 1)
ans=0
a,b=map(int,input().split())
W=[]
for i in range(b):
  s,t=map(int,input().split())
  for k in range(s,t+1):
    W.append(k)
for d in range(len(W)+1):
  V=W.count(d)
  if V==b:
    ans+=1
print(ans)
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)
left = max(l)
right = min(r)

if left >= right:
    print(0)
else:
    print(right - left + 1)
n,m=map(int,input().split())
left=0
right=10**5
for i in range(m):
  l,r=map(int,input().split())
  left=max(left,l)
  right=min(right,r)
print(right-left+1)
import numpy as np
n, m = map(int, input().split())


#card_list = np.zeros(n)

under = 1
top = n

for i in range(m):
    l, r = map(int, input().split())
    if under < l:
        under = l
    if top > r:
        top = r

#print(under, top)

print(top-under+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:
    print(right, R)
    right = R
print(right-left+1)
n,m=map(int,input().split())
ansl=1
ansr=n
for i in range(m):
    l,r=map(int,input().split())
    if ansl<l:
        ansl=l
    if ansr>r:
        ansr=r
print(ansr-ansl+1)
n, m = map(int, input().split())


l_max = 0
r_min = float('inf')

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

ans = int(r_min-l_max+1)
print(ans)
n,m = map(int, input().split())
l,r = [0]*m,[0]*m
for i in range(m):
    l[i],r[i] = map(int,input().split())
s = min(n,min(r)) - min(n,max(l))
print(s+1)
n,m=map(int,input().split())
a=[]
b=[]
for i in range(m):
    l,r=map(int,input().split())
    a.append(l)
    b.append(r)
x=max(a)
y=min(b)

print(y-x+1)
n, m = map(int, input().split())
l = 1
r = n
for _ in range(m):
    li, ri = map(int, input().split())
    l = max(l, li)
    r = min(r, ri)

print(r - l + 1 if r > 0 else 0)
N, M = map(int,input().split())


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

max_L = 0
min_R = 10**5

for m in range(M):
  if max_L > LR[m][0]:
    max_L = LR[m][0]
  if min_R <LR[m][1]:
    min_R = LR[m][1]
    

L = [x[0] for x in LR]
R = [x[1] for x in LR]

if max(L) - min(R) >=0:
  print(max(L)-min(R)+1)
else:
  print(0)
import numpy as np
n, m = map(int, input().split())
table = np.ones(n, dtype=int)
for i in range(m):
    l, r = map(int, input().split())
    table[:l-1] = 0
    table[r+1:] = 0
print(sum(table))
n, m = map(int, input().split())
L_max, R_min = map(int, input().split())
for i in range(m-1):
    l, r = map(int, input().split())
    L_max = max(L_max, l)
    R_min = min(R_min, r)

print(R_min - L_max + 1)
n, m = map(int, input().split())
left = 0
right = m+1
for i in range(m):
    l, r = map(int, input().split())
    left = max(left, l)
    right = min(right, r)
ans = 0
for i in range(left-1, right):
    ans += 1
print(ans)
n,m=map(int, input().split())
lr = [map(int, input().split()) for _ in range(m)]
l, r = [list(i) for i in zip(*lr)]
answer = (r[0]  - l[-1]  +1) 
print(answer)
N, M = map(int, input().split())
l_max = 1
r_min = N + 1
for _ in range(M):
    L, R = map(int, input().split())
    if l_max < L:
        l_max = L
    if r_min > R:
        r_min = R
if r_min < l_max:
    print(r_min - l_max + 1)
else:
    print(0)
N,M=map(int,input().split())
LR=[list(map(int,input().split())) for _ in range(M)]

l=0
r=10**5+1

for i in LR:
    if i[0]>l:
        l=i[0]
    if i[1]<r:
        r=i[1]

print(r-l+1)
N, M = list(map(int, input("").split(" ")))
permit = [list(map(int, input("").split(" "))) for i in range(M)]
able = [1 for i in range(N)]
l,r = 1, N
for i,j in permit:
  l = max(l, i)
  r = min(r, j)
print(r-l+1)
def main():
    N, M = map(int, input().split())
    Lmax  = 0
    Rmin  = N+1
    for i in range(M):
        L, R = map(int, input().split())
        if L > Lmax:
            Lmax = L
        if R < Rmin:
            Rmin = R
    print(Rmin - Lmax + 1)
main()
n,m = map(int,input().split())

min_i = 1
max_i = n

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

print(max_i-min_i+1)
N, M=map(int, input().split())  #複数数値入力　「A B」みたいなスペース空いた入力のとき
LR=[[int(i) for i in input().split()] for i in range(M)]  # 二次元配列入力　二次元マップみたいな入力のとき

mi, ma = 0, 10**5

for i in LR:
    mi = max(mi, i[0])
    ma = min(ma, i[1])

if ma-mi <= 0:
    print(0)
else:
    print(ma-mi + 1)
N, M = map(int, input().split())
L = 0
R = N
for i in range(M):
    l, r = map(int, input().split())
    L = max(L, l)
    R = min(R, r)
print(R - L + 1)
def main():
    N,M = map(int, input().split())
    l=-100000
    r=1000000
    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)
main()
n, m = map(int, input().split())
array = [list(map(int, input().split())) for i in range(m)]
result = [1, n]
for el in array:
    work = sorted(result + el)
    result = [work[1], work[2]]
    if result[0] > result[1]:
        result = [1, 0]
        break
print(result[1]-result[0]+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-min(rmin,lmax)+1)
if __name__ == '__main__':
    main()
N,M=map(int, input().split())
pass_id = [i for i in range(1, N+1)]

for i in range(M):
    L,R=map(int, input().split())
    for id in pass_id:
        if not id in range(L,R+1):
            pass_id.pop(pass_id.index(id))
print(len(pass_id))
n, m = map(int, input().split())
rg = []
for i in range(m):
    rg.append(list(map(int, input().split())))

ans = set(range(0, n))
for i in rg:
    ans &= set(range(i[0], i[1]))

print(len(ans)+1)
N,M=map(int,input().split())
G=[list(map(int,input().split())) for _ in range(M)]
min=0
max=float("inf")
for i in range(M):
    if G[i][0]>min:
        min=G[i][0]
    if G[i][1]<max:
        max=G[i][1]
print(max-min+1)
n, m = map(int, input().split())
a = 0
b = 10**5+1
for i in range(m):
    l, r = map(int, input().split())
    a = max(a, l)
    b = min(b, r)
print(b-a+1)
n,m = map(int,input().split())

num_conter = [1]*n

max_L = 0
max_R = 10**5+1

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

    if max_L < L:
        max_L = L
    if max_R > R:
        max_R = R
print(max_R-max_L+1)
def solve():
    import sys
    n, m, *LR = map(int, sys.stdin.read().split())
    L = LR[::2]
    R = LR[1::2]
    l = max(L)
    r = min(R)
    print(r - l + 1)


solve()
n, m = map(int, input().split())
l_max = 0
r_min = 10**6
for i in range(m):
    l, r = map(int, input().split())
    l_max = max(l_max, l)
    r_min = min(r_min, r)
print(r_min-l_max+1)
def main():
    import sys

    input = sys.stdin.readline

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

    L, R = 1, N
    for _ in range(M):
        left, right = map(int, input().split())
        L = max(L, left)
        R = min(R, right)
    ans = R - L + 1
    print(ans)


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())
n, m = map(int, input().split())
l0, r0 = 1, n
for j in range(m):
    l1, r1 = map(int, input().split())
    l0 = max(l0, l1)
    r0 = min(r0, r1)
print(r0-l0+1)
n, m = map(int, input().split())
ll = [0 for i in range(n+1)]
l_max = 0
r_min = 1000000
for _ in range(m):
    l, r = map(int, input().split())
    if l > l_max :
        l_max = l
    if r < r_min:
        r_min = r
print(r_min - l_max + 1)
n,m=map(int,input().split())
##かーどについての初期配列
head =0
tail = n
for i in range(m):
    l,r = map(int,input().split())
    head = max(head,l)
    tail = min(r,tail)
    
print(tail-head+1)
n, m = map(int, input().split())
l = 0
r = float('inf')
for i in range(m):
    li, ri = map(int, input().split())
    lb, rb = True, True
    if l <= li <= r:
        l = li
    elif li < l:
        pass
    else:
        lb = False
    if r >= ri >= l:
        r = ri
    elif ri > r:
        pass
    else:
        rb = False
    if not (lb or rb):
        print(0)
        exit()
    # print(l,r)

print(r - l + 1)
import sys
from math import ceil, floor, sqrt, sin, cos, pi
from itertools import accumulate, permutations, combinations
from fractions import gcd # 最大公約数
from collections import deque, Counter
from operator import itemgetter
from heapq import heappop,heappush
sys.setrecursionlimit(10**7)
def lcm(x, y): return ((x * y) // gcd(x, y)) # 最小公倍数
# list(map(int, input().split()))
n, m = map(int, input().split())
minv = 1
maxv = n
for _ in range(m):
    l, r = map(int, input().split())
    if maxv > r: maxv = r
    if minv < l: minv = l
print(maxv - minv + 1)
N, M = map(int, input().split())

lower = 0
upper = N+1
for _ in range(M):
    L, R = map(int, input().split())
    lower = max(lower, L)
    upper = min(upper, R+1)
print(upper - lower)
N,M = map(int,input().split())

bottom = 0
top = N+1

for i in range(M):
  L,R = map(int,input().split())
  bottom = max(bottom, L)
  top = min(top,R)
  
print(top - bottom + 1)
N, M = map(int, input().split())
L = sorted([list(map(int, input().split())) for _ in range(M)])

ml = 0
mm = 10**7
for v in L:
    ml = max(ml, v[0])
    mm = min(mm, v[1])

print(mm - ml + 1)
def resolve():
    N, M = [int(i) for i in input().split()]
    llrr = [list(map(int, input().split())) for _ in range(M)]
    maxL = 1
    minR = N
    for l, r in llrr:
        maxL = max(maxL, l)
        minR = min(minR, r)
    ans = minR - maxL + 1
    if ans < 0:
        print(0)
    else:
        print(ans)
import copy

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

l1 = []
l2 = []

for i in range(M):
  L, R = map(int, input().split())
  
  l1 = list(range(L,R+1))
  if len(l2) == 0:
    l2 = copy.copy(l1)
  ans = set(l1) & set(l2)
  l2 = copy.copy(ans)

print(len(ans))
n, m = map(int, input().split())
l = 1
r = m
for _ in range(m):
    li, ri = map(int, input().split())
    l = max(l, li)
    r = min(r, ri)

print(r - l + 1 if r >= 1 else 0)
import sys
sys.setrecursionlimit(10**6)

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

L = []; R = []
for _ 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
print(ans)
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
def main():
    n, m = map(int, readline().split())
    INF = int(1e5)+1
    mx = -1
    mi = INF 
    for _ in range(m):
        l, r = map(int, readline().split())
        mx = max(l, mx)
        mi = min(r, mi)
    print(mi-mx+1)
if __name__ == '__main__':
    main()
n,m = map(int,input().split())

max_L = []
min_R = []

cnt = 0
for i in range(m):
    L,R = map(int,input().split())
    max_L.append(L)
    min_R.append(R)
print(min(min_R)-max(max_L)+1)
n , m = map(int, input().split())
count = 0
low = 0
high  = n
for _ in range(m):
    l,r=map(int, input().split())
    low = max(l, low)
    high = min(high, r)
    #print(low, high)

print(high-low+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)
print(min(k_2)-max(k_1)+1)
N,M = map(int, input().split())
l=0
r=N
for _ in range(M):
  ll,rr=map(int,input().split())
  l = max(l,ll)
  r = min(r,rr)
print(r-l+1)
n,m = map(int,input().split())
l = []
r = []

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

print(min(r)-max(l)+1)
n, m = [int(i) for i in input().split()]
l = []
r = []
for _ in range(m):
    i, k = [int(i) for i in input().split()]
    l.append(i)
    r.append(k)

print(min(r) - max(l) + 1)
N,M=map(int,input().split())
ans=0
L,R=0,(10**5)+1
for i in range(M):
    l,r=map(int,input().split())
    L=max(L,l); R=min(R,r)
    if r<l: break
R=min(R,N)
print(abs((L-1)-R) if 0<R-L else 0)
N, M = map(int, input().split())
Lmax = 0
Rmin = 10**5 + 1
for n in range(M):
  L, R = map(int, input().split())
  if L > Lmax:
    Lmax = L
  if R < Rmin:
    Rmin = R

gate = Rmin - Lmax + 1
print(gate)
n,m = map(int,input().split())
gate = [[int(i) for i in input().split()] for _ in range(m)]

minv = gate[0][0]
maxv = gate[0][1]

for x in gate:
  minv = max(minv,x[0])
  maxv = min(maxv,x[1])
  
print(maxv-minv+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())
print(min(R)-max(L)+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(minR - maxL + 1, 0)
def main():
    n,m = map(int,input().split())
    
    lm = 1
    rm = n
    for i in range(m):
        l,r = map(int,input().split())
        lm = max(lm,l)
        rm = min(rm,r)
    
    print(rm-lm+1)

if __name__ == "__main__":
    main()
N,M= map(int,input().split())
gate=[list(map(int,input().split())) for i in range(M)]
count2=0

min1=0
max1=1000000000

for j in gate:
  if j[0]>min1:
    min1=j[0]
  if j[1]<max1:
    max1=j[1]
print(max1-min1+1)
N, M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]
l = 0
r = 100001
for lr in LR:
  l = max(lr[0], l)
  r = min(r, lr[1])
  
print(r - l + 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 = 1

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

print(count)
import collections
n,m = map(int,input().split())
card = [list(map(int,input().split())) for c in range(m)]
l = 0
r = n
for index,c in enumerate(card):
	if c[0] >= l:
		l = c[0]
	if c[1] <= r:
		r = c[1]
print(r-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)
print(min(R) - max(L) + 1)
n,m=map(int,input().split())
l=1;ma=n
te=set(range(1,n+1))
for i in range(m):
    x,y=map(int,input().split())
    l=max(x,l)
    ma=min(ma,y)
print(min(abs(l-ma)+1,m))
n, m = map(int, input().split())

s = [0] * (n+1)

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

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

print(ans.count(max(ans)))
# 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)

for i in range(M):
    if i == 0:
        maxL = l_list[i]
        minR = r_list[i]
        continue
    maxL = max(maxL, l_list[i])
    minR = min(minR, r_list[i])
ans = max(minR - maxL + 1, 0)
"""
i番目のゲートは LiからRiまでのIDカードのうちどれか1枚を持っていれば通過できます
"""

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

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

print(right-left+1)
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)
if rh-lh<0:
  print(0)
else:
  print(rh-lh+1)
n,m=map(int,input().split())
a=[]
b=[]
for i in range(m):
  f,g=map(int,input().split())
  a.append(f)
  b.append(g)
  
print(min(b)-max(a)+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(abs(upper-lower)+1)
N, M = map(int, input().split())
LR = [(map(int, input().split())) for i in range(M)]

L = 1
R = N
for i in range(M):
    a, b = LR[i]
    L = max(L,a)
    R = min(R,b)
print(R-L+1)
N, M = map(int, input().split())
min_ID = 0
max_ID = N
for _ in range(M):
    L, R = map(int, input().split())
    if min_ID < L:
        min_ID = L
    if max_ID > R:
        max_ID = R
print(max_ID - min_ID + 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)
print(min(R)-max(L)+1)
N,M = map(int,input().split())
L = []
R = []
count = 0
for j in range(M):
  l,r = map(int,input().split())
  L.append(l)
  R.append(r)
for i in range(N+1):
  if i > max(L) and i < min(R):
    count += 1
print(count)
# coding: utf-8
# Your code here!

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

for i in range(2,M+1):
    L,R = map(int,input().split())
    
    if (Li<=L and L<=Ri):
        Li = L
        Ri = min(Ri,R)
    elif (R>=Li and R<=Ri):
        Li = max(L,Li)
        Ri = R
    else:
        Li=1
        Ri=0
        break
        
print(Ri-Li+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)

a = 1
b = n
for i in range(m):
    a = max(l[i], a)
    b = min(r[i], b)

print(b - a + 1)
n,m=map(int,input().split())
l=1;ma=n
te=set(range(1,n+1))
for i in range(m):
    x,y=map(int,input().split())
    l=max(x,l)
    ma=min(ma,y)
    
print(abs(l-ma)+1)
n,m = list(map(int, input().split()))
high=10**10
low=0
for i in range(m):
    a,b = list(map(int, input().split()))
    if a>low:
        low=a
    if b<high:
        kigk=b
print(high-low+1)
N, M = map(int, input().split())
maxL = 1
minR = 1
for i in range(M):
  L, R = map(int, input().split())
  maxL = max(maxL, L)
  minR = min(minR, R)
print(max(minR - maxL +1, 0))
n,m = map(int, input().split())
a = [0,n]
for i in range(m):
  s,t = map(int, input().split())
  a[0] = max(a[0], s)
  a[1] = min(a[1], t)
print(a[1] - a[0] + 1)
n, m = list(map(int, input().split()))

list = [list(map(int, input().split())) for x in range(m)]

ans = [1]*n

s = 0
m = n

for i in list:
    if i[0] > s:
        s = i[0]
    if i[1] < m:
        m = i[1]
print(m-s+1)
n,m=map(int,input().split())
hidari=-10**100
migi=10**100
for i in range(m):
  l,r=map(int,input().split())
  if l>migi or r<hidari:
    keep=0
    break
  elif hidari<=l<=r<=migi:
    hidari=l
    migi=r
  elif l<=hidari<=migi<=r:
    pass
  elif l<=hidari<=r<=migi:
    migi=r
  elif hidari<=l<=migi<=r:
    hidari=l
    
res=migi-hidari+1       
print(min(res,n))
# coding: utf-8
# Your code here!

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

for i in range(2,M+1):
    L,R = map(int,input().split())
    
    if (L<=Ri and R>=Ri):
        Li = L
    elif (R>=Li and Li>=L):
        Ri = R
    else:
        Li=1
        Ri=0
        break
        
print(Ri-Li+1)
N, M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]

lList = [arr[0] for arr in LR]
rList = [arr[1] for arr in LR]

l = max(lList)
r = min(rList)

print(r-l+1)
n,m = map(int,input().split())
a = 1
b = n
for i in range(m):
    l,r = map(int,input().split())
    a = max(a,l)
    b = min(b,r)
print(b-a+1)
n,m=map(int,input().split())
L=[]
M=[]
for i in range(m):
  l,m=map(int,input().split())
  L.append(l)
  M.append(m)
  
a=max(L)
b=min(M)
print(b-a+1)
N,M = map(int, input().split())
L,R = map(int, input().split())

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

print(R-L+1)
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)
print(min(r_lst) - max(l_lst) + 1)
N,M= map(int, input().split())
L,R= map(int, input().split())
flag = True
for i in range(M-1):
    a,b= map(int, input().split())
    if a >= L and b <= R:
        L = a
        R = b
    elif L <= b and R >= b:
        R = b
    elif L <= a and R >= a:
        L = a
    else:
        flag = False
        break
if flag:
 print(R-L+1)
else:
    print(0)