CodeEff/ECCO huggingface dataset:
# Original solution
N, M=list(map(int, input().split()))
lst=[list(map(int, input().split())) for _ in range(M)]
ans=set(i for i in range(N))
for L, R in lst:
  L, R=L-1, R-1
  ans-=set(i for i in range(L))
  ans-=set(i for i in range(R+1, N))
print((len(ans)))

# Optimized solution
N, M=list(map(int, input().split()))
lst=[list(map(int, input().split())) for _ in range(M)]
maxL, minR=-1, N
for L, R in lst:
  L, R=L-1, R-1
  maxL=max(maxL, L)
  minR=min(minR, R)
if minR>=maxL:
  print((minR-maxL+1))
else:
  print((0))
# Original solution
import sys
sys.setrecursionlimit(10**7)

def judge(c,l,r,counter):
  if counter == 0:
    return c
  elif len(c) == 0:
    return c
  else:
    return judge(list([x for x in c if l[0]<=x<=r[0]]),l[1:],r[1:],counter-1)

def solve(c,l,r,m):
  return len(judge(c,l,r,m))
  
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()))
cards = list(range(1,N+1))
print((solve(cards,L,R,M)))


# Optimized solution
def solve(l,r):
  l_max = max(l)
  r_min = min(r)
  return max(r_min - l_max + 1, 0)
  
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((solve(L,R)))
# Original solution
import os, sys, re, math

(N, M) = [int(n) for n in input().split()]

cards = set([(i + 1) for i in range(N)])
for i in range(M):
    (L, R) = [int(n) for n in input().split()]
    if min(cards) > R or max(cards) < L:
        cards = {}
        break
    cards -= set([(i + 1) for i in range(L - 1)])
    cards -= set([(i + 1) for i in range(R, N)])

print((len(cards)))


# Optimized solution
import os, sys, re, math

(N, M) = [int(n) for n in input().split()]

minC = 1
maxC = N
for i in range(M):
    (L, R) = [int(n) for n in input().split()]
    minC = max(minC, L)
    maxC = min(maxC, R)

print((max((maxC - minC + 1), 0)))
# Original solution
#!/usr/bin/env python3

def main():
    N, M = list(map(int, input().split()))
    Card = [[] for i in range(M)]
    Diff = [i for i in range(1, N + 1)]

    for i in range(0, M):
        L, R = list(map(int, input().split()))
        for j in range(L, R + 1):
            Card[i].append(j)
        Diff = list(set(Diff) & set(Card[i]))
    print((len(Diff)))


main()

# Optimized solution
#!/usr/bin/env python3

def main():
    N, M = list(map(int, input().split()))
    Diff = [i for i in range(1, N + 1)]

    for i in range(0, M):
        L, R = list(map(int, input().split()))
        Card = []
        for j in range(L, R + 1):
            Card.append(j)
        Diff = list(set(Diff) & set(Card))
    print((len(Diff)))


main()
# Original solution
N, M = [int(x) for x in input().split(' ')]

P = {}
for i in range(M):
  l,r = [int(x) for x in input().split(' ')]
  for s in range(l,r+1):
    if s not in P:
      P[s] = 0
    P[s] += 1
    #print(s)
    
cnt = 0
for k,v in list(P.items()):
  if v == M:
    cnt += 1
print(cnt)

# Optimized solution
N, M = [int(x) for x in input().split(' ')]

L = 0
R = N
for i in range(M):
  l,r = [int(x) for x in input().split(' ')]
  if L < l:
    L = l
  if r < R:
    R = r

cnt = 0
if L <= R:
  cnt = R - L + 1
print(cnt)
# Original 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()))
ans = 0
A = [0] * N
for i in range(M):
    for j in range(L[i]-1,R[i]):
        A[j] += 1
        if A[j] == M:
            ans += 1
print(ans)

# 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()))
ans = 0
A = [0] * N
L_max = max(L)
R_min = min(R)
if R_min >= L_max:
    ans = R_min - L_max + 1
else:
    ans = 0
print(ans)
# Original solution
N,M = list(map(int,input().split()))
L = []
R = []
count = 0
for j in range(M):
  l,r = list(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)

# Optimized solution
N,M = list(map(int,input().split()))
L = []
R = []
count = 0
for j in range(M):
  l,r = list(map(int,input().split()))
  L.append(l)
  R.append(r)
print((max(0,min(R)-max(L)+1)))
# Original solution
N, M = list(map(int, input().split(" ")))
acc_gates = set([i for i in range(1, N+1)])
for m in range(M):
    i, j = list(map(int, input().split(" ")))
    tmp = set([n for n in range(i, j+1)])
    acc_gates = acc_gates & tmp

print((len(set(acc_gates))))

# Optimized solution
N, M = list(map(int, input().split(" ")))
min_l = 1
max_r = N

for m in range(M):
    l, r = list(map(int, input().split(" ")))
    if l > min_l:
        min_l = l
    if r < max_r:
        max_r = r

ans = (max_r-min_l)+1
if ans < 0:
    ans = 0

print(ans)
# Original solution
n,m = list(map(int,input().split()))
lr = [[0]*2]*m
for i in range(m):
  lr[i] = list(map(int,input().split()))
valid = set(range(1,n+1))
for i in range(m):
  subleft = set(range(1,lr[i][0]))
  subright = set(range(lr[i][1]+1,n+1))
  valid.difference_update(subleft)
  valid.difference_update(subright)
print((len(valid)))

# Optimized solution
n,m = list(map(int,input().split()))
lr = [[0]*2]*m
for i in range(m):
  lr[i] = list(map(int,input().split()))
maxl = 0
minr = 10**5
for i in range(m):
  maxl = max(maxl,lr[i][0])
  minr = min(minr,lr[i][1])
if maxl>minr:
  print((0))
else:
  print((minr-maxl+1))
# Original solution
import collections
N,M = list(map(int,input().split()))


s = []
count = 0
for _ in range(M):
  a,b = list(map(int,input().split()))
  if M==1:
    print((b-a+1))
    exit()
  for i in range(a,b+1):
    s.append(i)
    k = collections.Counter(s)
    
for d in list(k.values()):
    if d==M:
      count+=1
      
print(count)

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

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

card_list = [1] * n

for i in range(m):
    l, r  = list(map(int, input().split()))
    l -= 1
    r -= 1
    for j in range(n):
        if l <= j <= r:
            continue
        card_list[j] = 0
print((sum(card_list)))


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

card_list = [1] * n

minr = 10**9
maxl = 0

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
def main():
    N, M = list(map(int, input().split()))
    cards = list(range(1, N + 1))
    for i in range(M):
        L, R = list(map(int, input().split()))
        cards = [x for x in cards if L <= x <= R]
    print((len(cards)))


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    N, M = list(map(int, input().split()))
    L, R = 0, 1001001
    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)))


if __name__ == '__main__':
    main()
# Original solution
N,M=list(map(int,input().split()))
B=[0]*N
for i in range(M):
    l,r=list(map(int,input().split()))
    for j in range(l-1,r):
        B[j]+=1
print((B.count(M)))

# Optimized solution
N,M=list(map(int,input().split()))
ml=0
mh=N
for i in range(M):
    l,r=list(map(int,input().split()))
    ml=max(ml,l)
    mh=min(mh,r)
print((max(0,mh-ml+1)))
# Original solution
n, m = list(map(int, input().split()))
l_lst = list()
for i in range(m):
    l_lst.append(input().split())

for i in range(0, len(l_lst)):
    for j in range(0, len(l_lst[i])):
        l_lst[i][j] = int(l_lst[i][j])

temp_cnt = 0
cnt = 0
for i in range(1, n+1):
    # print(i)
    for j in range(0, len(l_lst)):
        if l_lst[j][0] <= i and i <= l_lst[j][1]:
            temp_cnt += 1
        if temp_cnt == len(l_lst):
            cnt += 1
            # temp_cnt = 0
    temp_cnt = 0

print(cnt)

# Optimized solution
n, m = list(map(int, input().split()))
l_lst = list()
for i in range(m):
    l_lst.append(input().split())

for i in range(0, len(l_lst)):
    for j in range(0, len(l_lst[i])):
        l_lst[i][j] = int(l_lst[i][j])

max_lst = list()
min_lst = list()
for i in range(0, len(l_lst)):
    max_lst.append(l_lst[i][0])
    min_lst.append(l_lst[i][1])

max_int = max(max_lst)
min_int = min(min_lst)

cnt = 0
for i in range(1, n+1):
    if max_int <= i and i <= min_int:
        cnt += 1

print(cnt)
# Original solution
from sys import stdin

N, G = list(map(int, stdin.readline().split()))
dic = {key: 0 for key in range(1, N + 1)}
left, right = N+1, -1
this, next = set(), set()
for i in range(G):
    if i == 0:
        l, r = list(map(int, stdin.readline().split()))
        for j in range(l, r + 1):
            this.add(j)
    else:
        l, r = list(map(int, stdin.readline().split()))
        for j in range(l, r + 1):
            if j in this:
                next.add(j)
        this = next
        next = set()

print((len(this)))


# Optimized solution
from sys import stdin, exit

N, G = list(map(int, stdin.readline().split()))
left, right = 1, N
for i in range(G):
    l, r = list(map(int, stdin.readline().split()))
    if l > left:
        left = l
    if r < right:
        right = r
    if right < left:
        print((0))
        exit()

print((right - left + 1))
# Original 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()))

keys = list(range(1, N+1))
common = [x for x in keys if x>=max(L) and x<=min(R)]
    
print((len(common)))

# 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
n,m=list(map(int,input().split()))
a=[]
for i in range(m):
    a.append(list(map(int,input().split())))
ans=n
for i in range(1,n+1):
    for j in range(m):
        if i<a[j][0] or a[j][1]<i:
            ans=ans-1
            break

print(ans)


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

s=[]
l= 0
r = n
for i in range(m):
    ln,rn=list(map(int,input().split()))
    if ln>l:
        l=ln
    if rn<r:
        r=rn
if r-l+1>0:
    print((r-l+1))
else:
    print((0))
# Original solution
n,m=list(map(int,input().split()))
count=0
s=[0]*(n+1)
 
for i in range(m):
  l,r=list(map(int,input().split()))
  for j in range(l,r+1):
    s[j]+=1
    
for i in range(n+1):
  if s[i]==m:
    count+=1
    
print(count)

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

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

print((len(gate)))

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

mi, mx = list(map(int, input().split()))
flag = False
for i in range(m-1):
    l, r = list(map(int, input().split()))
    if mi < l <= mx:
        mi = l
    if mi <= r < mx:
        mx = r
    if mx < l or r < mi:
        flag = True
        break

if flag:
    print((0))
else:
    print((mx - mi + 1))
# Original solution
N, M = list(map(int, input().split()))
L, R = list(map(int, input().split()))
pool = set(range(L, R+1))
for i in range(M-1):
  L, R = list(map(int, input().split()))
  pool = set(range(L, R+1)).intersection(pool)
print((len(pool)))

# Optimized solution
N, M = list(map(int, input().split()))
L1, R1 = list(map(int, input().split()))
flag = 0
def find_key(L1,R1,L2,R2):
    if R1 < L2 or R2 < L1:
        return(False)
    else:
        a = [R1,L1,R2,L2]
        a.sort()
        return(a[1],a[2])

for i in range(M - 1):
    L,R = list(map(int, input().split()))
    if find_key(L1,R1,L,R) != False:
        L1,R1 = find_key(L1,R1,L,R)
    else:
        flag = 1
        print((0))
        break
if flag == 0:
    print((R1-L1+1))
# Original solution
n,M=list(map(int,input().split()))
lr=[list(map(int,input().split())) for i in range(M)]
cnt=0
for i in range(1,n+1):
  sub=0
  for m in range(M):
    if i>=lr[m-1][0] and i<=lr[m-1][1]:sub+=1
    if sub==M:cnt+=1
#    print('i:',i,'l:',lr[m-1][0],'r:',lr[m-1][1],'sub:',sub)
print(cnt)


# Optimized solution
n,m=list(map(int,input().split()))
L,R=0,n
for _ in range(m):
  l,r=list(map(int,input().split()))
  L,R=max(L,l),min(R,r)
print((max(R-L+1,0)))
# Original solution
n,m =list(map(int,input().split()))
a=[1]*n
b=[0]*n
for i in range(m):
  l,r=list(map(int,input().split()))
  if l==1:
    a[r:]=b[r:]
  else:
    a[:l-1]=b[:l-1]
    a[r:]=b[r:]
print((sum(a)))

# 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)
if l>r:
    print((0))
else:   
    print((r-l+1))
# Original solution
n, m = list(map(int, input().split()))
l = 0
r = n
for i in range(m):
    lt, rt = list(map(int, input().split()))
    l = max(l, lt)
    r = min(r, rt)
if(l<=r):
    print((r - l + 1))
else:
    print((0))


# Optimized solution
n, m = list(map(int, input().split()))
l = 0
r = n
for i in range(m):
    lt, rt = list(map(int, input().split()))
    if (lt > l):
        l = lt
    if (rt < r):
        r = rt
if (r <l):
    print((0))
else:
    print((r-l+1))
# Original solution
N,M=list(map(int,input().split()))
x={i for i in range(10**5+1)}
for _ in range(M):
    L,R=list(map(int,input().split()))
    tmp={i for i in range(L,R+1)}
    x=x&tmp
print((len(x)))

# Optimized 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)
Lm=max(L)
Rm=min(R)
print((Rm-Lm+1 if Rm-Lm>=0 else 0))
# Original solution
n, m = list(map(int, input().split()))
lr = [list(map(int, input().split())) for _ in range(m)]

tmp = [0] * n

def add_one(x):
    return x + 1

for i in range(m):
    l = lr[i][0] - 1
    r = lr[i][1] - 1
    tmp[l: r+1] = list(map(add_one, tmp[l: r + 1]))

counter = 0
for j in range(n):
    if tmp[j] == m:
        counter += 1

print(counter)


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

l_list = [_lr[0] for _lr in lr]
r_list = [_lr[1] for _lr in lr]

a = min(r_list)
b = max(l_list)
print((max(a - b + 1, 0)))
# Original solution
n,m = list(map(int, input().split()))
keys = []
for i in range(m):
  l,r = list(map(int, input().split()))
  keys.append([l,r])
cnt = 0
for key in range(1, n+1):
  for x, y in keys:
    if key < x or key > y:
      cnt -= 1
      break
  cnt += 1
print(cnt)

# Optimized solution
n,m = list(map(int, input().split()))
l_lst, r_lst = [], []
for i in range(m):
  l,r = list(map(int, input().split()))
  l_lst.append(l)
  r_lst.append(r)
res = min(r_lst)-max(l_lst)+1
if res > 0:
  print(res)
else:
  print((0))
# Original solution
n,m = list(map(int,input().split(" ")))
card = {i+1 for i in range(n)}

for i in range(m):
    l,r = list(map(int,input().split(" ")))
    tmp = set()
    for j in range(l,r+1):
        tmp.add(j)

    card = card.intersection(tmp)
print((len(card)))

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

x,y = list(map(int,input().split(" ")))
for i in range(m-1):
    l,r = list(map(int,input().split(" ")))
    x = max(x,l)
    y = min(y,r)

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

# Optimized solution
n,m = list(map(int,input().split()))
key = [i+1 for i in range(n)]
lmax = 0
rmin = 10**5
for i in range(m):
  l,r = list(map(int,input().split()))
  lmax = max(l,lmax)
  rmin = min(r,rmin)

num = rmin-lmax+1
print((num if num>0 else 0))
# Original solution
inf = input().split()
N = int(inf[0])
M = int(inf[1])
ls = []
m = 0
for i in range(M):
  inf = input().split()
  L = int(inf[0])-1
  R = int(inf[1])-1
  x = max(L,R)
  if x > m:
    m = x
  ls += [(L,R)]

ans = [True]*(m+1)

for s in ls:
  ans[:s[0]] = [False]*len(ans[:s[0]])
  ans[s[1]+1:] = [False]*len(ans[s[1]+1:])

print((sum(ans)))

# Optimized solution
inf = input().split()
N = int(inf[0])
M = int(inf[1])
ls = []
m = 0
pair = [0,N]
for i in range(M):
  inf = input().split()
  L = int(inf[0])
  R = int(inf[1])
  pair[0] = max(pair[0], L)
  pair[1] = min(pair[1], R)
  if pair[0]>pair[1]:
    print((0))
    break
else:
  print((pair[1]-pair[0]+1))
# Original solution
N,M=list(map(int,input().split()))
A=list(range(N))
ans=0
for i in range(M):
  L,R=list(map(int,input().split()))
  for j in range(N):
    if A[j]+1<L or R<A[j]+1:
      A[j]=-1
for k in range(N):
  if A[k]!=-1:
    ans=ans+1
print(ans)

# Optimized solution
N,M=list(map(int,input().split()))
ans,L,R=0,0,N
for i in range(M):
  Ltmp,Rtmp=list(map(int,input().split()))
  if L<Ltmp:
    L=Ltmp
  if R>Rtmp:
    R=Rtmp
if L<=R:
  ans=R-L+1
print(ans)
# Original solution
N,M = list(map(int,input().split()))
X = [list(map(int,input().split())) for n in range(M)]
L = [0]*N

for m in range(M):
  for g in range(X[m][0],X[m][1]+1):
    L[g-1]+=1
    
print((L.count(M)))

# Optimized solution
N,T = list(map(int,input().split()))
X = [list(map(int,input().split())) for n in range(T)]
M = N
m = 0

for a,b in X:
  M = min(M,b)
  m = max(m,a)
  
if M<m:
  print((0))
else:
  print((M-m+1))
# Original solution
n, m = list(map(int, input().split()))

l = [nu for nu in range(1, n + 1)]

for i in range(m):
    L, R = list(map(int, input().split()))
    card = [nu for nu in range(L, R + 1)]
    l = list(set(l) & set(card))

print((len(l)))



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


for _ in range(m - 1):
    l_, r_ = list(map(int, input().split()))
    l = max(l, l_)
    r = min(r, r_)


print((max(r - l + 1, 0)))
# Original solution
from sys import stdin
N, M = [int(x) for x in stdin.readline().rstrip().split()]
data = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(M)]
for i in range(M):
  id = list(range(data[i][0],data[i][1]+1))
  if i == 0:
      num = id
  else:
      num = set(num) & set(id)
print((len(num)))

# Optimized solution
from sys import stdin
N, M = [int(x) for x in stdin.readline().rstrip().split()]
data = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(M)]
min_id =0
max_id = 100000
for i in range(M):
  min_id = max(min_id,data[i][0])
  max_id = min(max_id,data[i][1])
num = max_id - min_id + 1 if max_id - min_id >= 0 else 0
print(num)
# Original solution

def main():
    n, m = list(map(int, input().split()))
    l = []
    r = []
    ans = 0
    for i in range(m):
        a, b = list(map(int, input().split()))
        l.append(a)
        r.append(b)
    for i in range(1, n+1):
        if i >= max(l) and i <= min(r):
            ans += 1
    print(ans)


if __name__ == '__main__':
    main()

# Optimized solution
def main():
    n, m = list(map(int, input().split()))
    l = []
    r = []
    ans = 0
    for i in range(m):
        a, b = list(map(int, input().split()))
        l.append(a)
        r.append(b)
    ml = max(l)
    mr = min(r)
    for i in range(1, n+1):
        if i >= ml and i <= mr:
            ans += 1
    print(ans)


if __name__ == '__main__':
    main()
# Original solution
from collections import defaultdict
n,m=list(map(int,input().split()))
d = defaultdict(int)
for i in range(m):
  s, e = list(map(int,input().split()))
  for j in range(s,e+1):
    j = str(j)
    d[j] += 1
#print(d)
count = 0
for k,v in list(d.items()):
  if v==m:
    count+=1
print(count)

# Optimized solution
n,m=list(map(int,input().split()))
mi = 0
ma = 10**5+1
for i in range(m):
  s,e = list(map(int,input().split()))
  if s > mi:
    mi = s
#    print('mi',mi)
  if e < ma:
    ma = e
#    print('ma',ma)
print((max(ma-mi+1,0)))
# Original solution
n,m=list(map(int,input().split()))
mi = 0
ma = 10**5+1
for i in range(m):
  s,e = list(map(int,input().split()))
  if s > mi:
    mi = s
#    print('mi',mi)
  if e < ma:
    ma = e
#    print('ma',ma)
print((max(ma-mi+1,0)))

# Optimized solution
n,m=list(map(int,input().split()))
mir = n
mal = 0
for _ in range(m):
  l,r = list(map(int,input().split()))
  if l > mal:
    mal = l
  if r < mir:
    mir = r
print((0 if mir-mal < 0 else (mir-mal)+1))
# Original solution
# -*- coding: utf-8 -*-
# import numpy as np

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

t = [0]*n

for i in range(m):
    l, r = list(map(int, input().split()))
    for j in range(n):
        if l<=j+1 and r>=j+1:
            t[j] += 1

print((t.count(m)))

# Optimized solution
# -*- coding: utf-8 -*-
# import numpy as np

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

l_min = 1
r_max = n+1

for i in range(m):
    l, r = list(map(int, input().split()))
    l_min = max(l_min, l)
    r_max = min(r_max, r)

ans = r_max - l_min + 1
if ans > 0:
    print(ans)
else:
    print((0))
# Original solution
n,m=list(map(int,input().split()))

cards=set([i+1 for i in range(n)])


for j in range(m):

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

    gates = set([j for j in range(l,r+1)])

    cards = cards&gates


print((len(cards)))



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

    l_i,r_i = list(map(int,input().split()))

    l = max(l,l_i)
    r = min (r,r_i)

if r>=l:
    print((r-l+1))
else:
    print((0))
# Original solution
import sys  
input = sys.stdin.readline

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


k = [0]*N
for i in range(M):
    l, r = list(map(int, input().split()))
    for q in range(l-1, r):
        k[q] += 1

ans = 0
for k_ in k:
    if k_ == M:
        ans +=1

print(ans)

# Optimized solution
import sys  
input = sys.stdin.readline

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



ans = -1
l_min , r_max = list(map(int, input().split()))
for i in range(M-1):
    l, r = list(map(int, input().split()))
    if r_max < l or r < l_min:
        ans = 0
        break

    if l_min <= l and l <= r_max:
        l_min = l

    if l_min <= r and r <= r_max:
        r_max = r


if ans == 0:
    print(ans)
else:
    print((r_max - l_min + 1))
# Original solution
import sys
input = sys.stdin.readline
N,M = list(map(int, input().split()))
cards = [1] * N
for _ in range(M):
    l,r = list(map(int, input().split()))
    for i in range(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()))
l = 0
r = N
for m in range(M):
    L,R = list(map(int, input().split()))
    if L > l:
        l = L
    if R < r:
        r = R
print((max(r-l+1,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)
print((max(min(R)-max(L)+1,0)))

# Optimized solution
import sys
N,M = list(map(int, input().split()))
D = sys.stdin
L = []
R = []
for d in D:
    l,r = list(map(int, d.split()))
    L.append(l)
    R.append(r)
print((max(min(R)-max(L)+1,0)))
# Original solution
import sys
N,M = list(map(int, input().split()))
D = sys.stdin
L = []
R = []
for d in D:
    l,r = list(map(int, d.split()))
    L.append(l)
    R.append(r)
print((max(min(R)-max(L)+1,0)))

# Optimized solution
import sys

N,M = list(map(int, input().split()))
D = sys.stdin
l = 0
r = N
for d in D:
    L,R = list(map(int, d.split()))
    if L > l:
        l = L
    if R < r:
        r = R
print((max(r-l+1,0)))
# Original solution
#ABC 127 C Prison
N,M= list(map(int,input().split()))
ans=None
for i in range(M):
    L,R= list(map(int,input().split()))
    tmp=set([i for i in range(L,R+1)])
    if ans is None:
        ans=tmp
    else:
        ans=ans & tmp
print((len(ans)))

# Optimized solution
#ABC 127 C Prison
N,M= list(map(int,input().split()))
MIN=0
MAX=N
for i in range(M):
    L,R= list(map(int,input().split()))
    MAX=min(MAX,R)
    MIN=max(MIN,L)
ans =MAX-MIN+1
if ans <0:
    print((0))
else:
    print(ans)