CodeEff/ECCO huggingface dataset:
# Original solution
n,m = list(map(int,input().split()))
numSet = set(range(1,n+1))
for i in range(m):
  l,r = list(map(int,input().split()))
  numSet &= set(range(l,r+1))
print((len(numSet)))


# Optimized solution
n,m = list(map(int,input().split()))
numSet = set(range(1,n+1))
L,R = [],[]
for i in range(m):
  l,r = list(map(int,input().split()))
  L.append(l)
  R.append(r)
if min(R) >= max(L):
  print((min(R)-max(L)+1))
else:
  print((0))
# Original solution
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

n, m = list(map(int, input().split()))
a = [0] * (n + 2)
r = defaultdict(int)
for i in range(m):
    x,y = list(map(int,input().split()))
    a[x - 1] += 1
    a[y] += -1

for i in range(n + 1):
    a[i + 1] += a[i]
count = 0
flg = True
for i in range(n):
    if a[i] >= m:
        count += 1
print(count)

# Optimized solution
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *
readl= lambda: list(map(int, sys.stdin.readline().split()))
readt= lambda: tuple(map(int, sys.stdin.readline().split()))
read = lambda: sys.stdin.readline().rstrip()
readi = lambda: int(read())
readmi = lambda: list(map(int, sys.stdin.readline().split()))
readms = lambda: list(map(str, sys.stdin.readline().split()))

n, m = readmi()
a = [0] * (n + 2)
r = defaultdict(int)
for i in range(m):
    x,y = readmi()
    a[x - 1] += 1
    a[y] += -1

for i in range(n + 1):
    a[i + 1] += a[i]
count = 0
flg = True
for i in range(n):
    if a[i] >= m:
        count += 1
print(count)
# Original solution
n, m = list(map(int, input().split()))
l, r = list(map(int, input().split()))
card = set(range(l, r + 1))
for _ in range(1, m):
    l, r = list(map(int, input().split()))
    card &= set(range(l, r + 1))

print((len(card)))

# Optimized solution
n, m = list(map(int, input().split()))
lr = [tuple(map(int, input().split())) for _ in range(m)]
maxl = max(_[0] for _ in lr)
minr = min(_[1] for _ in lr)
if minr - maxl >= 0:
    answer = minr - maxl + 1
else:
    answer = 0
print(answer)
# Original solution
n, m = list(map(int, input().split()))

ans = set(range(1, n+1))

for i in range(m):
  l, r = list(map(int, input().split()))
  ans = ans & set(range(l, r+1))

print((len(ans)))

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

maxl = 0
minr = 10**5
for i in range(m):
  l, r = list(map(int, input().split()))
  maxl = max(maxl, l)
  minr = min(minr, r)

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

l, r = list(map(int, input().split()))
ans = set(range(l, r+1))
for i in range(m-1):
    l, r = list(map(int, input().split()))
    ans = ans & set(range(l, r + 1))
print((len(ans)))


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

l, r = 0, 10 ** 6
for i in range(m):
    tl, tr = list(map(int, input().split()))
    l = max(tl, l)
    r = min(tr, r)
print((len(list(range(l, r+1)))))
# Original solution
n, m = map(int,input().split())
n_lis = [i+1 for i in range(n)]
 
for j in range(m):
  l, r = map(int,input().split())
  n_lis = [k for k in n_lis if k >= l and k <= r]
 
print(len(n_lis),flush=True)


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

a = r_p - l_p + 1

if a > 0:
  print(a)
else:
  print((0))
# Original solution
N, M = list(map(int, input().split()))
 
hashset = None
 
for _ in range(M):
  L, R = list(map(int, input().split()))
  if hashset is None:
    hashset = set(range(L, R+1))
  else:
    hashset = hashset & set(range(L, R+1))

print((len(hashset)))

# Optimized solution
N, M = list(map(int, input().split()))
 
left, right = 1, N
 
for _ in range(M):
  L, R = list(map(int, input().split()))
  
  left = max(L, left)
  right = min(R, right)

ans = right - left + 1 if left <= right else 0 
print(ans)
# Original solution
N, M = list(map(int,input().split()))
ans = 0

keylist = [[False]*M for _ in range(N)]

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

for i in range(N):
    if all(keylist[i]):
        ans += 1
        
print(ans)

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

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

print((max(0,R-L+1)))
# Original solution
n, m = list(map(int, input().split()))
lr = [list(map(int, input().split())) for _ in range(m)]
ans = []
for i in range(m):
    res = []
    for j in range(lr[i][0], lr[i][1] + 1):
        res.append(j)
    if i == 0:
        ans = set(res)
    else:
        ans = set(ans) & set(res)
print((len(ans)))


# Optimized solution
n, m = list(map(int, input().split()))
mi, ma = 1, n
for _ in range(m):
    l, r = list(map(int, input().split()))
    mi = max(l, mi)
    ma = min(r, ma)
print((max(0, ma - mi + 1)))
# Original solution
n, m = list(map(int, input().split()))
data = []
for i in range(m):
    data.append(list(map(int, input().split())))
num = 1
count = 0
while num <= n:
    flag = True
    for i in range(m):
        if data[i][0] > num or data[i][1] < num:
            flag = False
    num += 1
    if flag:
        count += 1
print(count)

# Optimized solution
n, m = list(map(int, input().split()))
data = []
data1 = []
data2 = []
for i in range(m):
    data.append(list(map(int, input().split())))
for i in range(m):
    data1.append(data[i][0])
    data2.append(data[i][1])
if min(data2) - max(data1)  + 1 > 0:
    print((min(data2) - max(data1) + 1))
else:
    print((0))
# Original solution
n,m=list(map(int,input().split()))
ID=[0 for i in range(n)]
for i in range(m):
    l,r=list(map(int,input().split()))
    for j in range(r-l+1):
        ID[l-1+j]+=1
print((ID.count(m)))

# 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)
print((min(R)-max(L)+1 if min(R)-max(L)+1>0 else 0))
# Original solution
N,M = list(map(int,input().split()))
lists = []
for i in range(M):
    L,R = list(map(int,input().split()))
    lists.append(list(range(L,R+1)))
if len(lists) == 1:
    print((len(lists[0])))
    exit()
for i in range(M-1):
    lists[i+1] = set(lists[i]) & set(lists[i+1])
    ans = lists[-1]
print((len(ans)))

# 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 = list(range(max(L),min(R)+1))
print((len(ans)))
# Original solution
n, m = list(map(int, input().split()))
#b = list(map(int, input().split()))
dic = {}
for i in range(m):
    l, r = [int(j) for j in input().split()]
    for j in range(l, r+1):
        if str(j) in dic:
            dic[str(j)] += 1
        else:
            dic[str(j)] = 1

count = 0
for i in list(dic.values()):
    if i == m:
        count += 1

print(count)


# Optimized solution
n, m = list(map(int, input().split()))
#b = 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)
L = l[0]
R = r[0]
# print("--------")
for i in range(1, m):
    if not((R < l[i] and R < r[i]) or (r[i] < L and l[i] < L)):
        L = max(L, l[i])
        R = min(R, r[i])
    else:
        print((0))
        exit()
    #print(L, R)

print((R-L+1))
# Original solution
n, m = list(map(int, input().split()))
line = set(range(1, n + 1))
for i in range(m):
    l, r = list(map(int, input().split()))
    line &= set(range(l, r + 1))

print((len(line)))


# Optimized solution
n, m = list(map(int, input().split()))
l_max = 1
r_min = 10 ** 5
for i in range(m):
    l, r = list(map(int, input().split()))
    l_max = max(l, l_max)
    r_min = min(r, r_min)

print((max(0, r_min - l_max + 1)))
# Original solution
from collections import defaultdict


class Solution:
    def main(self):
        N, M = list(map(int, input().split()))
        L = []
        R = []
        cards = set(list(range(1, N+1)))
        for _ in range(M):
            l, r = list(map(int, input().split()))
            cands = set(list(range(l, r+1)))
            cards = cards & cands
        print((len(cards)))


if __name__ == '__main__':
    Solution().main()

# Optimized solution
from collections import defaultdict


class Solution:
    def main(self):
        N, M = list(map(int, input().split()))
        L = []
        R = []
        cards = set(list(range(1, N+1)))
        left = 1
        right = N
        for _ in range(M):
            l, r = list(map(int, input().split()))
            if right < l or r < left:
                print((0))
                return 0

            left = max(l, left)
            right = min(r, right)
        else:
            print((right-left+1))
            return


if __name__ == '__main__':
    Solution().main()
# Original solution
import sys

def main():
    sinputl = sys.stdin.readline

    num_card, num_gate = [int(x) for x in sinputl().split()]
    input = [[int(x) for x in sinputl().split()] for _ in range(num_gate)]

    min_interval = [1, num_card]

    for gate in input:
        if (min_interval[1] - min_interval[0]) > (gate[1] - gate[0]):
            min_interval = gate

    cards = list(range(min_interval[0], min_interval[1] + 1))

    for gate in input:
        cards = list([x for x in cards if gate[0] <= x <= gate[1]])

    print((len(cards)))



if __name__ == "__main__":
    main()


# Optimized solution
import sys

def main():
    sinputl = sys.stdin.readline

    num_card, num_gate = [int(x) for x in sinputl().split()]
    input = [[int(x) for x in sinputl().split()] for _ in range(num_gate)]

    card = [1, num_card]

    for gate in input:
        card[0] = max(card[0], gate[0])
        card[1] = min(card[1], gate[1])

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


if __name__ == "__main__":
    main()
# Original solution
n, m = list(map(int, input().split()))
d = [0]*(10**5)
for _ in range(m):
    l, r = list(map(int, input().split()))
    for i in range(l-1, r):
        d[i] += 1
ans = 0
for i in d:
    if i == m:
        ans += 1
print(ans)

# Optimized solution
n, m = list(map(int, input().split()))
l, r = list(map(int, input().split()))
for _ in range(1, m):
    l_, r_ = list(map(int, input().split()))
    l = max(l, l_)
    r = min(r, r_)
if r-l<0:
    print((0))
else:
    print((r-l+1))
# Original solution
n,m = list(map(int,input().split()))
for i in range(m):
    l,r=list(map(int,input().split()))
    if i == 0:
        ans = set(range(l,r+1))
    else:
        ans = ans & set(range(l,r+1))
print((len(ans)))


# Optimized solution
n,m = list(map(int,input().split()))
la = 0
ra = n
for i in range(m):
    l,r=list(map(int,input().split()))
    la = max(la,l)
    ra = min(ra,r)
print((max(0,ra-la+1)))
# Original solution
(n, m) = list(map(int, input().split()))
counts = [0 for _ in range(n)]
for _ in range(m):
    gate_i = list(map(int, input().split()))
    for i in range(gate_i[0] - 1, gate_i[1]):
        counts[i] += 1
counts.sort()
ans = 0
for count in counts:
    ans += 1 if m == count else 0
print(ans)


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


def intersect(lhs, rhs):
    if lhs[1] < rhs[0] or rhs[1] < lhs[0]:
        return [0, 0]
    min_value = lhs[0] if lhs[0] > rhs[0] else rhs[0]
    max_value = lhs[1] if lhs[1] < rhs[1] else rhs[1]
    return [min_value, max_value]


ans = [-1, 2 * 10 ** 5]
for _ in range(m):
    ans = intersect(ans, list(map(int, input().split())))
    if 0 == ans[0] and 0 == ans[1]:
        print((0))
        exit(0)
print((ans[1] - ans[0] + 1))
# Original solution
N, M = list(map(int, input().split()))

L, R = list(map(int, input().split()))
hoge = set(list(range(L, R+1)))

for i in range(1, M):
    L, R = list(map(int, input().split()))
    hoge = hoge & set(list(range(L, R+1)))

print((len(hoge)))


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

L, R = list(map(int, input().split()))

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


if L > R:
    print((0))
else:
    print((R-L+1))
# Original solution
N, M = list(map(int, input().split()))
imos = [0]*(N+1)
for _ in range(M):
    # ゲートの情報が入ってくる
    l, r = [int(x)-1 for x in input().split()]
    imos[l] += 1
    imos[r+1] -= 1  # rまでは有効であり、r+1以降が無向になる仕様であるのでr+1をデクリメントする

cumulative = [imos[0]]
for value in imos[1:]:
    cumulative.append(cumulative[-1]+value)
ans = 0
for c in cumulative:
    if c == M:
        ans += 1
print(ans)


# Optimized solution
N, M = list(map(int, input().split()))
left = -1
right = 10**10

for _ in range(M):
    l, r = list(map(int, input().split()))
    left = max(left, l)
    right = min(right, r)

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

gates = []
for _ in range(m):
    gates.append(tuple(map(int, input().split())))

gates.sort(key=lambda x: (x[0], x[1]), reverse=True)

count = 0
for i in range(1, n+1):
    for gate in gates:
        if i < gate[0] or gate[1] < i :
            break
    else:
        count += 1
print(count)


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

gates = []
start, end = 1, n
for _ in range(m):
    L, R = list(map(int, input().split()))
    if L > start:
        start = L
    if R < end:
        end = R

print((len(list(range(start, end+1)))))
# Original solution
n,m=list(map(int,input().split(" ")))
l_init,r_init=list(map(int,input().split(" ")))
ans=list(range(l_init,r_init+1))
for i in range(m-1):
	l,r=list(map(int,input().split(" ")))
	ans=list(set(ans)&set(range(l,r+1)))
if len(ans)>n:
	print(n)
else:
	print((len(ans)))

# Optimized solution
n,m=list(map(int,input().split(" ")))
l_init,r_init=list(map(int,input().split(" ")))
ans_l=l_init
ans_r=r_init
isPossible=True
for i in range(m-1):
	l,r=list(map(int,input().split(" ")))
	isupdated=False
	next_ans_l=ans_l
	next_ans_r=ans_r
	if ans_l <= l and l <= ans_r:
		next_ans_l=l
		isupdated=True
	if ans_l <= r and r <= ans_r:
		next_ans_r=r
		isupdated=True
	if l <= ans_l and ans_r <= r:
		isupdated=True
	if not isupdated:
		isPossible=False
		break
	ans_l=next_ans_l
	ans_r=next_ans_r
ans=ans_r-ans_l
if isPossible:
	print((ans+1))
else:
	print((0))
# Original solution
n,m=list(map(int,input().split()))
data=[list(map(int,input().split())) for i in range(m)]
import sys
input = lambda: sys.stdin.readline()[:-1]
l,r=0,10**6
for i,j in data:
	l=max(l,i)
	r=min(r,j)
print((max(0,r-l+1)))



# Optimized solution
#!/usr/bin/env python3
import sys
input = lambda: sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**8)
inf = float('inf')
mod = 10**9+7


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





l, r = 0, 10**6
for i, j in data:
	l = max(l, i)
	r = min(r, j)
print((max(0, r-l+1)))
# Original solution
#!/usr/bin/env python3
import sys
input = lambda: sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**8)
inf = float('inf')
mod = 10**9+7


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





l, r = 0, 10**6
for i, j in data:
	l = max(l, i)
	r = min(r, j)
print((max(0, r-l+1)))


# Optimized solution
#!/usr/bin/env python3
import sys
input = lambda: sys.stdin.buffer.readline()[:-1]
sys.setrecursionlimit(10**8)
inf = float('inf')
mod = 10**9+7


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





l, r = 0, 10**6
for i, j in data:
	l = max(l, i)
	r = min(r, j)
print((max(0, r-l+1)))
# Original solution
from sys import stdin


def fetch_one_line():
    return stdin.readline().rstrip()


def fetch_int_input():
    return [int(s) for s in fetch_one_line().split()]


def fetch_inputs(times):
    return [fetch_one_line() for _ in range(times)]


def fetch_int_inputs(times):
    return [fetch_int_input() for _ in range(times)]


N, M = fetch_int_input()
gate_len = fetch_int_inputs(M)
range_card = [[False] * N for _ in range(M)]

for gl, rc in zip(gate_len, range_card):
    l = gl[0] - 1
    r = gl[1] - 1
    rc[l: r + 1] = [True] * (r - l + 1)

range_card = list(zip(*range_card))
print((sum([all(r) for r in range_card])))


# Optimized solution
from sys import stdin


def fetch_one_line():
    return stdin.readline().rstrip()


def fetch_int_input():
    return [int(s) for s in fetch_one_line().split()]


def fetch_inputs(times):
    return [fetch_one_line() for _ in range(times)]


def fetch_int_inputs(times):
    return [fetch_int_input() for _ in range(times)]


N, M = fetch_int_input()
gate_len = fetch_int_inputs(M)
# range_card = [[False] * N for _ in range(M)]
l, r = 0, N
for gl in gate_len:
    if l < gl[0]:
        l = gl[0]
    if r > gl[1]:
        r = gl[1]

# range_card = list(zip(*range_card))
# print(sum([all(r) for r in range_card]))
result = r - l + 1
if result < 0: result = 0

print(result)
# Original solution
n, m = list(map(int, input().split()))
num = [list(map(int, input().split())) for _ in range(m)]
count = 0

for i in range(1, n+1):
  ans = 0
  for j in range(m):
    if i >= num[j][0] and i <= num[j][1]:
      ans += 1
    else:
      break
  if ans == m:
    count += 1

print(count)

# Optimized solution
n, m = list(map(int, input().split()))
num = [list(map(int, input().split())) for _ in range(m)]
num1 = 1
num2 = n

for j in range(m):
  num1 = max(num[j][0], num1)
  num2 = min(num[j][1], num2)

ans = num2 - num1 + 1

if ans > 0:
  print(ans)
else:
  print((0))
# Original solution
N, M = list(map(int, input().split()))
gate = [True for i in range(N+1)]

for i in range(M):
    r, m = list(map(int, input().split()))
    gate[:r] = [False] * r
    gate[m+1:] = [False] * (N-m+1)

print((sum(gate)))

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

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

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

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

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

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

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
n, m = list(map(int, input().split()))
count = [0]*(n+1)
for i in range(m):
    l, r = list(map(int, input().split()))
    for j in range(l, r+1):
        count[j] += 1
        
print((count.count(m)))

# Optimized solution
N, M = list(map(int, input().split()))
l, r = -1, N+1
for _ in range(M):
    L, R = list(map(int, input().split()))
    l = max(l, L)
    r = min(r, R)
ans = max(0, r-l+1)
print(ans)
# Original solution
N,M = list(map(int, input().split()))
LR = [list(map(int,input().split())) for _ in range(M)]
gate_list = []
ans = set(range(LR[0][0], LR[0][1]+1))
for i in range(M):
    gate_list = set(range(LR[i][0], LR[i][1]+1))
    ans = gate_list & ans
print((len(ans)))

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

ans = min(gol) -max(sta) 
print((ans+1 if ans >= 0 else 0))
# Original solution
import collections
n,m = list(map(int, input().split()))
cnt = [0 for _ in range(n)]
for i in range(m):
	l,r = list(map(int, input().split()))
	for i in range(l-1, r):
		cnt[i] += 1
c = collections.Counter(cnt)
print((c[m]))

# Optimized solution
n,m = list(map(int, input().split()))
minn, maxn = 1, n
for i in range(m):
	l, r = list(map(int,input().split()))
	minn, maxn = max(minn, l), min(maxn, r)
print((max(maxn-minn+1, 0)))
# Original solution
N, M = list(map(int, input().split()))
gates = []
res = 0
for _ in range(M):
  L, R = list(map(int, input().split()))
  gates.append((L, R))

for i in range(1, N+1):
  flag = True
  for g in gates:
    if g[0] <= i <= g[1]:
      continue
    else:
      flag = False
      break 
  if flag: res+=1
  
print(res)

# Optimized solution
N, M = list(map(int, input().split()))
res = 0
L_max = 0
R_min = 100000
for _ in range(M):
  L, R = list(map(int, input().split()))
  if L_max < L: L_max = L
  if R < R_min: R_min = R
  
  
print((max(0, (R_min - L_max + 1))))
# Original solution
n,m=list(map(int,input().split()))
l=[]
h=[]
count=0
for i in range(m):
    a,b=list(map(int,input().split()))
    l.append(a)
    h.append(b)

for i in range(n):
    judge=True
    for j in range(m):
        #if not (((i+1) >= l[j]) and ((i+1) <= h[j])):
        if not ((i+1) >= l[j] and (i+1) <= h[j]):
            judge^=True
            break
    if judge:
        count+=1

print(count)

# Optimized solution
n,m=list(map(int,input().split()))
l=[]
h=[]
count=0
for i in range(m):
    a,b=list(map(int,input().split()))
    l.append(a)
    h.append(b)

l_max=max(l)
h_min=min(h)
if (h_min-l_max)<0:
    print((0))
else:
    print((h_min-l_max+1))
# Original solution
N, M = list(map(int, input().split()))
idcard = [[int(j) for j in input().split()] for i in range(M)]
ans = {}
if M == 1:
    print((idcard[0][1] - idcard[0][0] + 1))
    exit()
t = 0
for i in idcard:
    for j in range(i[0], i[1] + 1):
        if j in ans:
            ans[j] += 1
        else:
            ans[j] = 1
        t = max(ans.values())
w = 0
for i in list(ans.values()):
    if i == t:
        w += 1

print(w)

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

L = 0
R = 10**10
for i in idcard:
    L = max(L, i[0])
    R = min(R, i[1])
if R >= L:
    print((R - L + 1))
else:
    print((0))
# Original solution
N, M = list(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] = list(map(int, input().split()))

cards = list(range(N + 1))
u = set(cards[L[0]:R[0] + 1])
for i in range(1, M):
    u = u & set(cards[L[i]:R[i] + 1])
print((len(u)))

# Optimized solution
N, M = list(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] = list(map(int, input().split()))

l = max(L)
r = min(R)
print((r - l + 1 if l <= r else 0))
# Original solution
n,m,*t=list(map(int,open(0).read().split()));a,c=[0]*-~n,0
for l,r in zip(t[::2],t[1::2]):a[l-1]+=1;a[r]-=1
for i in range(n):a[i+1]+=a[i];c+=a[i]==m
print(c)

# Optimized solution
n,m,*t=list(map(int,open(0).read().split()));print((max(0,min(t[1::2])-max(t[::2])+1)))
# Original solution
n, m = list(map(int, input().split()))
l, r = list(map(int, input().split()))
tmp_l = [i for i in range(l, r + 1)]
tmp_set = set(tmp_l)
for _ in range(m - 1):
    l, r = list(map(int, input().split()))
    tmp_l = [i for i in range(l, r + 1)]
    tmp_set = tmp_set.intersection(set(tmp_l))
 
print((len(tmp_set)))

# Optimized solution
n, m = list(map(int, input().split()))
l_lst = [0] * m
r_lst = [0] * m
for i in range(m):
    l, r = list(map(int, input().split()))
    l_lst[i] = l
    r_lst[i] = r

if max(l_lst) <= min(r_lst):
    print((min(r_lst) - max(l_lst) + 1))
else:
    print((0))
# Original solution
N, M = list(map(int, input().split()))
l, r = list(map(int, input().split()))
cards = list(range(l, r+1))

for i in range(1, M):
    l, r = list(map(int, input().split()))
    cards_new = list(range(l, r+1))
    cards = set(cards) & set(cards_new)

print((len(cards)))

# Optimized solution
N, M = list(map(int, input().split()))
l_max, r_min = 0, 100000000

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

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