qyliang/CodeNanoFix huggingface dataset:
# Buggy code:
n,m=map(int,input().split())

lamp=[]
switches=[]
temp=[]

for i in range(m):
    temp=list(map(int,input().split()))
    lamp.append(temp.pop(0))
    switches.append(temp)
    temp=[]

modul=list(map(int,input().split()))

bit_list=[]

for i in range(2**(n)):
    bit_list.append(bin(i)[2:].zfill(n))

ans = 0

for b in bit_list:
    flag = 0
    sum  = 0
    for s,p in zip(switches,modul):
        for k in range(len(s)):
            sum=sum+int(b[s[k]-1])
        if sum%2 != p:
            flag = 1
            break
    if flag == 0:
        ans=ans+1

print(bit_list)
print(ans)


# Fixed code:
n,m=map(int,input().split())

lamp=[]
switches=[]
temp=[]

for i in range(m):
    temp=list(map(int,input().split()))
    lamp.append(temp.pop(0))
    switches.append(temp)
    temp=[]

modul=list(map(int,input().split()))

bit_list=[]

for i in range(2**(n)):
    bit_list.append(bin(i)[2:].zfill(n))

ans = 0

for b in bit_list:
    flag = 0
    for s,p in zip(switches,modul):
        sum = 0
        for k in range(len(s)):
            sum=sum+int(b[s[k]-1])
        if sum%2 != p:
            flag = 1
            break
    if flag == 0:
        ans=ans+1

print(ans)
# Buggy code:
N, M = map(int, input().split())
switch_list=[input().split() for i in range(M)]
switch_list=[[int(i[m+1]) for m in range(len(i)-1)] for i in switch_list]
P_list=input().split()
P_list=[int(i) for i in P_list]
for i in range(1 << z):
    switch=[0 for i in range(N)]
    for j in range(z):
        if ((i >> j) & 1) == 1:
            switch[j]=1
    count+=sum([sum([switch[m-1] for m in switch_list[x]])%2==P_list[x] for x in range(M)])==M
print(count)


# Fixed code:
N, M = map(int, input().split())
switch_list=[input().split() for i in range(M)]
switch_list=[[int(i[m+1]) for m in range(len(i)-1)] for i in switch_list]
P_list=input().split()
P_list=[int(i) for i in P_list]
count=0
z=N
for i in range(1 << z):
    switch=[0 for i in range(N)]
    for j in range(z):
        if ((i >> j) & 1) == 1:
            switch[j]=1
    count+=sum([sum([switch[m-1] for m in switch_list[x]])%2==P_list[x] for x in range(M)])==M
print(count)
# Buggy code:
n, m = map(int,input().split())
lis = [list(map(int,input().split())) for i in range(m)]
p = list(map(int,input().split()))
count = 0
for i in range(n**2):
    switch = [0]*n
    for j in range(n):
        if ((i >> j) & 1):
            switch[j] = 1
    for k in range(m):
        on = 0
        All = True
        for l in range(len(lis[k][1:])):
            on += switch[lis[k][1+l]-1]
        if on % 2 == p[k]:
            continue
        else:
            All = False
            break
    if All == True:
        count += 1
        
print(count)

# Fixed code:
n, m = map(int,input().split())
lis = [list(map(int,input().split())) for i in range(m)]
p = list(map(int,input().split()))
count = 0
for i in range(2**n):
    switch = [0]*n
    for j in range(n):
        if ((i >> j) & 1):
            switch[j] = 1
    All = True
    for k in range(m):
        on = 0        
        for l in lis[k][1:]:
            on += switch[l-1]
        if on % 2 == p[k]:
            continue
        else:
            All = False
            break
    if All == True:
        count += 1
        
print(count)
# Buggy code:
def resolve():
n, m = map(int, input().split())
denq = []
for _ in range(m):
    ks = tuple(map(lambda x: int(x)-1, input().split()))
    denq.append(ks[1:])
P = tuple(map(int, input().split()))

ans = 0
for i in range(2**n):
    kk = [0] * m
    # switch_states = [(i>>j)&1 for j in range(n)]
    for idq, dq in enumerate(denq):
        counts = 0
        for s in dq:
            counts += (i>>s)&1
        if counts%2 != P[idq]:
            break
    else:
        ans += 1
print(ans)


# Fixed code:
n, m = map(int, input().split())
denq = []
for _ in range(m):
    ks = tuple(map(lambda x: int(x)-1, input().split()))
    denq.append(ks[1:])
P = tuple(map(int, input().split()))

ans = 0
for i in range(2**n):
    kk = [0] * m
    switch_states = [(i>>j)&1 for j in range(n)]
    for idq, dq in enumerate(denq):
        counts = 0
        for s in dq:
            counts += switch_states[s]
        if counts%2 != P[idq]:
            break
    else:
        ans += 1
print(ans)
# Buggy code:
n,m = map(int,input().split())
ss = []
for _ in range(m):
  s = list(map(int,input().split()))[1:]
  ss.append()
ps = list(map(int,input().split()))

def get_bit(i,n):
  res = []
  for i in range(n):
    res.append(i%2)
    i = int(i/2)
  return res[::-1]

ans = 0
for i in range(2**n):
  flags = get_bit(i,n)
  for j in range(m):
    count = 0
    for k in ss[j]:
      count += flags[k-1]
    if count %2 == ps[j]:
      continue
    else:
      break
  else:
    ans += 1
print(ans)

# Fixed code:
n,m = map(int,input().split())
ss = []
for _ in range(m):
  s = list(map(int,input().split()))[1:]
  ss.append(s)
ps = list(map(int,input().split()))

def get_bit(i,n):
  res = []
  for _ in range(n):
    res.append(i%2)
    i = int(i/2)
  return res[::-1]

ans = 0
for i in range(2**n):
  flags = get_bit(i,n)
  for j in range(m):
    count = 0
    for k in ss[j]:
      count += flags[k-1]
    if count %2 == ps[j]:
      continue
    else:
      break
  else:
    ans += 1
print(ans)
# Buggy code:
N, M = map(int, input().split())
S = []
for m in range(M):
    s = list(map(int, input().split()))
    S.append(s[1:])
P = list(map(int, input().split()))

ans = 0
for c in product([0, 1], repeat=N):
    ok = True
    for m in range(M):
        sm = 0
        for s in S[m]:
            sm += c[s - 1]
        if sm % 2 != P[m]:
            ok = False
            break
    if ok:
        ans += 1
print(ans)

# Fixed code:
from itertools import product
N, M = map(int, input().split())
S = []
for m in range(M):
    s = list(map(int, input().split()))
    S.append(s[1:])
P = list(map(int, input().split()))

ans = 0
for c in product([0, 1], repeat=N):
    ok = True
    for m in range(M):
        sm = 0
        for s in S[m]:
            sm += c[s - 1]
        if sm % 2 != P[m]:
            ok = False
            break
    if ok:
        ans += 1
print(ans)
# Buggy code:
N, M = map(int, input().split())
S = [0] * M
for i in range(M):
    _, *s, = map(int, input().split())
    for v in s:
        S[i] = S[i] | (2 << v)

*P, = map(int, input().split())

ans = 0
for p in range(2**N):
    ok = True
    for i in range(M):
        b = format(p & S[i], 'b')
        if b.count('1') % 2 != P[i]:
            ok = False
            break
    if ok:
        ans += 1

print(ans)

# Fixed code:
N, M = map(int, input().split())
S = [0] * M
for i in range(M):
    _, *s, = map(int, input().split())
    for v in s:
        S[i] = S[i] | (1 << (v - 1))

*P, = map(int, input().split())

ans = 0
for p in range(2**N):
    ok = True
    for i in range(M):
        b = format(p & S[i], 'b')
        if b.count('1') % 2 != P[i]:
            ok = False
            break
    if ok:
        ans += 1

print(ans)
# Buggy code:
def bits2tuple(n):
    ret = []
    i = 0
    while (1 << i) <= n:
        if n & (1 << i) != 0:
            ret.append(i)
        i += 1
    return tuple(ret)


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

    kM = []
    skM = []
    for _ in range(M):
        sk = list(map(int, input().split()))
        k = sk.pop(0)

        kM.append(k)
        skM.append(sk)

    pM = list(map(int, input().split()))

    ans = 0
    for bits in range(1, 1 << N):
        on_switch = list(map(lambda x: x + 1, bits2tuple(bits)))

        all_on = True
        for p, k, sk in zip(pM, kM, skM):
            if 0 < len(set(on_switch) & set(sk)) and len(set(on_switch) & set(sk)) % 2 != p:
                all_on = False
                break

        if all_on:
            ans += 1

    print(ans)


main()


# Fixed code:
def bits2tuple(n):
    ret = []
    i = 0
    while (1 << i) <= n:
        if n & (1 << i) != 0:
            ret.append(i)
        i += 1
    return tuple(ret)


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

    kM = []
    skM = []
    for _ in range(M):
        sk = list(map(int, input().split()))
        k = sk.pop(0)

        kM.append(k)
        skM.append(sk)

    pM = list(map(int, input().split()))

    ans = 0
    for bits in range(0, 1 << N):
        on_switch = list(map(lambda x: x + 1, bits2tuple(bits)))

        all_on = True
        for p, k, sk in zip(pM, kM, skM):
            if len(set(on_switch) & set(sk)) % 2 != p:
                all_on = False
                break

        if all_on:
            ans += 1

    print(ans)


main()
# Buggy code:
N, M = map(int, input().split())
L, R = map(int, input().split())
if M == 1:
    print(R - L + 1)
for _ in range(M-1):
    l, r = map(int, input().split())
    if l <= R and L <= r:
        L = max(L, l)
        R = min(R, r)
    else:
        print(0)
        exit()
print(R - L + 1)


# Fixed code:
N, M = map(int, input().split())
L, R = map(int, input().split())
if M == 1:
    print(R - L + 1)
    exit()
for _ in range(M-1):
    l, r = map(int, input().split())
    if l <= R and L <= r:
        L = max(L, l)
        R = min(R, r)
    else:
        print(0)
        exit()
print(R - L + 1)
# Buggy code:
n,m=map(int,input().split())
l=1
r=n
for i in range(m):
  li,ri=map(int,input().split())
  l = max(l,li)
  r = min(r,ri)
print(max(r-l,0)

# Fixed code:
n,m=map(int,input().split())
l=1
r=n
for i in range(m):
  li,ri=map(int,input().split())
  l = max(l,li)
  r = min(r,ri)
print(max(r-l+1,0))
# Buggy code:

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


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

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



# Fixed code:
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(max(min(R)-max(L)+1,0))
# Buggy code:
n, m = map(int, input().split())
p = []
lr = [list(map(int, input().split())) for _ in range(m)]
left = [lr[i][0] for i in range(m)]
right = [lr[i][0] for i in range(m)]
ans = max(left)-min(right)
print(ans+1 if ans >= 0 else 0)

# Fixed code:
n, m = map(int, input().split())
lr = [list(map(int, input().split())) for _ in range(m)]
left = [lr[i][0] for i in range(m)]
right = [lr[i][1] for i in range(m)]
ans = min(right) - max(left)
print(ans+1 if ans >= 0 else 0)
# Buggy code:
n,m= input().split()
n = int(n)
m = int(m)
g=[input().split() for i in range(m)]

for i in int(m):
  av =[0,0]
  av[0] = max(int(g[i][0]),av[0])
  av[1] = min(int(g[i][1]),av[1])
print(av[1] -av[0]+1)

# Fixed code:
n,m= input().split()
n = int(n)
m = int(m)
g=[input().split() for i in range(m)]
av =[-1,1000000]
for i in range(m):
  av[0] = max(int(g[i][0]),av[0])
  av[1] = min(int(g[i][1]),av[1])
print(max(av[1] -av[0]+1,0))
# Buggy code:
N, M = map(int, input().split(" "))
min_l = 1
max_r = N

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

# Fixed code:
N, M = map(int, input().split(" "))
min_l = 1
max_r = N

for m in range(M):
    l, r = 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)
# Buggy code:
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 min(r)-max(l)<0:
    print(0)
else:
    print(min(r)-max(l)+1

# Fixed code:
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 min(r)-max(l) < 0:
    print(0)
else:
    print(min(r)-max(l)+1)
# Buggy code:
N, M = map(int, input().split())

l_list = []
r_list = []
for i in range(M):
  l, r = map(int, input().split())
  l_list.append(l)
  r_list.append(r)
  
l = max(l_list)
r = min(r_list)

if r < l:
  print(0)
else:
  print(l-r+1)

# Fixed code:
N, M = map(int, input().split())
 
l_list = []
r_list = []
for i in range(M):
  l, r = map(int, input().split())
  l_list.append(l)
  r_list.append(r)
  
l = max(l_list)
r = min(r_list)
 
if r < l:
  print(0)
else:
  print(r-l+1)
# Buggy code:
n, m = map(int,input().split())

lefts = [0] * m
rights = [0] * m

for i in range(m):
	l, m = map(int,input().split())
	lefts[i] = l
	rights[i] = r

lefts_max = max(lefts)
rithts_min = min(rights)

result = rights_min - lefts_max + 1

print(max(result,0))



# Fixed code:
n, m = map(int,input().split())

lefts = [0] * m
rights = [0] * m

for i in range(m):
	l, r = map(int,input().split())
	lefts[i] = l
	rights[i] = r

lefts_max = max(lefts)
rights_min = min(rights)

result = rights_min - lefts_max + 1

print(max(result,0))
# Buggy code:
    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)]
     
        under_limit = [x[0] for x in input]
        over_limit = [x[1] for x in input]
     
        print(max(min(over_limit) - max(under_limit) + 1, 0))
     
     
    if __name__ == "__main__":
        main()

# Fixed code:
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)]
   
    under_limit = [x[0] for x in input]
    over_limit = [x[1] for x in input]
     
    print(max(min(over_limit) - max(under_limit) + 1, 0))
     
     
if __name__ == "__main__":
    main()
# Buggy code:
n,m = map(int, input().split())
lmax,rmin=0,n+1
for i in range(m):
  l,r=map(int, input().split()
  lmax=max(l,lmax)
  rmin=min(r,rmin)
print(max(0,lmax-rmin+1)

# Fixed code:
n,m = map(int, input().split())
lmax,rmin=0,n+1
for i in range(m):
  l,r=map(int, input().split())
  lmax=max(l,lmax)
  rmin=min(r,rmin)
print(max(0,rmin-lmax+1))
# Buggy code:
N, M = map(int, input().split())
L = 0
R = 100000
for i in range(M):
  Li, Ri = map(int, input().split())
  if Li > L:
    L = Li
  if Ri < R:
    R = Ri
print(L - R + 1)

# Fixed code:
N, M = map(int, input().split())
L = 1
R = N
for i in range(M):
  Li, Ri = map(int, input().split())
  if Li > L:
    L = Li
  if Ri < R:
    R = Ri
if L <= R:
  print(R - L + 1)
else:
  print(0)
# Buggy code:
n, m = map(int, input().split())
l = [0]*m
r = [0]*m
for i in range(m):
  p, q = map(int, input().split())
  l[i], r[i] = map(int, input().split())
print(max(min[r] - max[l] + 1, 0))

# Fixed code:
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(max(min(r) - max(l) + 1, 0))
# Buggy code:
n, m = map(int, input().split())
l, r = 0, m
for _ in range(m):
    l_in, r_in = map(int, input().split())
    l = max(l, l_in)
    r = min(r, r_in)
print(max(r-l+1, 0))


# Fixed code:
n, m = map(int, input().split())
l, r = 0, n
for _ in range(m):
    l_in, r_in = map(int, input().split())
    l = max(l, l_in)
    r = min(r, r_in)
print(max(r-l+1, 0))
# Buggy code:
n,m = map(int,input().split())
ls = [i for i in range(m)]
rs = [i for i in range(m)]
cnt = 0
for _ in range(m):
    ls[_],rs[_] = map(int,input().split())

maxl = max(ls)
minr = min(rs)

for nn in range(1,n+1):
    if maxl <= nn or minr >= nn:
        cnt += 1

print(cnt)

# Fixed code:
n,m = map(int,input().split())
ls = [i for i in range(m)]
rs = [i for i in range(m)]
cnt = 0
for _ in range(m):
    ls[_],rs[_] = map(int,input().split())

maxl = max(ls)
minr = min(rs)

for nn in range(1,n+1):
    if maxl <= nn and minr >= nn:
        cnt += 1
print(cnt)
# Buggy code:
N, M = map(int, input().split())
l = []
r = []
for _ in range(M):
  s, t = map(int, input().split())
  l.append(s)
  r.append(t)
  
lm = max(l)
rm = min(r)
print(max[rm - lm + 1, 0])

# Fixed code:
N, M = map(int, input().split())
l = []
r = []
for _ in range(M):
  s, t = map(int, input().split())
  l.append(s)
  r.append(t)
  
lm = max(l)
rm = min(r)
print(max([rm - lm + 1, 0]))
# Buggy code:
N, M = (int(x) for x in input().split())
L = []
R = []
for i in range(M):
	l, r = (int(x) for x in input().split())
	L.append(l)
	R.append(r)
	
lmax = max(L)
rmin = min(R)
ans = lmax - rmin + 1

print(ans)

# Fixed code:
N, M = (int(x) for x in input().split())
L = []
R = []
for i in range(M):
	l, r = (int(x) for x in input().split())
	L.append(l)
	R.append(r)
	
lmax = max(L)
rmin = min(R)
ans = rmin - lmax + 1
if ans < 0:
	ans = 0

print(ans)
# Buggy code:
n, m = map(int, input().split())

import numpy as np

rets = np.array([0] * n)
lmax = n
rmin = 0
for _ in range(m):
  l, r = map(int, input().split())
  lmax = max(l, lmax)
  rmin = min(r, rmin)
print(rmin - lmax + 1 if rmin - lmax + 1 > 0 else 0)




# Fixed code:
n, m = map(int, input().split())

import numpy as np

rets = np.array([0] * n)
lmax = 1
rmin = n
for _ in range(m):
  l, r = map(int, input().split())
  lmax = max(l, lmax)
  rmin = min(r, rmin)
print(rmin - lmax + 1 if rmin - lmax + 1 > 0 else 0)
# Buggy code:
import numpy as np
n,m=map(int,input().split())
post_b = 0
post_c = n
for i in range(m):
    b,c=map(int,input().split())
    if post_b < b:
        post_b = b
    if post_c > c:
        post_c = c

print(c-b+1)


# Fixed code:
import numpy as np
n,m=map(int,input().split())
post_b = 0
post_c = n
for i in range(m):
    b,c=map(int,input().split())
    if post_b < b:
        post_b = b
    if post_c > c:
        post_c = c

if (post_c - post_b + 1)<0:
    print("0")
else:
    print(post_c - post_b + 1)
# Buggy code:
n,m = map(int,input().split())

s = 0
e = n-1

for _ in range(m):
  l,r = map(int,input().split())
  
  s = max(s,l)
  e = min(e,r)
  
print(max(e-s+1,0))

# Fixed code:
n,m = map(int,input().split())

s = 1
e = n

for _ in range(m):
  l,r = map(int,input().split())
  
  s = max(s,l)
  e = min(e,r)
  
print(max(e-s+1,0))
# Buggy code:
def prison():
    N, M = (int(i) for i in input().split())
    L_max = 0
    R_min = float("inf")
    for _ in range(M):
        L, R = (int(i) for i in input().split())
        L_max = max(L, L_max)
        R_min = min(R, R_min)
	result = max(R_min - L_max + 1, 0)
    return result


print(prison())

# Fixed code:
def prison():
    N, M = (int(i) for i in input().split())
    L_max = 0
    R_min = float("inf")
    for _ in range(M):
        L, R = (int(i) for i in input().split())
        L_max = max(L, L_max)
        R_min = min(R, R_min)
        result = max(R_min - L_max + 1, 0)
    return result


print(prison())
# Buggy code:
N, M = map(int,input().split())
L = []
R = []
for i in range(M):
    tmp_L, tmp_R = input().split()
    L.append(tmp_L)
    R.append(tmp_R)

left = max(L)
right = min(R)

if right - left < 0:
    print(0)
else:
    print(right - left + 1)

# Fixed code:
N, M = map(int,input().split())
L = []
R = []
for i in range(M):
    tmp_L, tmp_R = map(int,input().split())
    L.append(tmp_L)
    R.append(tmp_R)

left = max(L)
right = min(R)

if right - left < 0:
    print(0)
else:
    print(right - left + 1)
# Buggy code:
N,M = map(int,input().split())
L_list = []
R_list = []

for i in range(M):
  L,R = map(int,input().split())
  L_list.append(L)
  R_list.append(R)
  
if max(R_list) >= max(L_list):
  print(max(R_list)  - max(L_list) + 1)
else:
  print(0)

# Fixed code:
N,M = map(int,input().split(" "))
L_list = []
R_list = []
for i in range(M):
    L,R = map(int,input().split(" "))
    L_list.append(L)
    R_list.append(R)
if min(R_list) - max(L_list) >= 0:
    print(min(R_list) - max(L_list) +1)
else:
    print(0)
# Buggy code:
n,m = map(int,input().split())
ll = []
rl = []

for i in range(m):
  l,r = list(map(int,input().split()))
  ll.append(l)
  rl.append(r)
  
if min(rr)<max(ll):
  print("0")

else:
  print(abs(max(rr)-min(ll)+1))

# Fixed code:
n,m = map(int,input().split())
ll = []
rl = []
 
for i in range(m):
  l,r = list(map(int,input().split()))
  ll.append(l)
  rl.append(r)
  
if min(rl)<max(ll):
  print("0")
 
else:
  print(abs(min(rl)-max(ll)+1))
# Buggy code:


v = [int(d) for d in input().split()]

LM = []
for d in range(v[1]):
   LM.append([int(d) for d in input().split()])

L = [d[0] for d in LM]
M = [d[1] for d in LM]
L.sort()
M.sort()
if M[0] <= L[-1]:
    if M[0] <= v[0]:
        print(M[0]-L[-1]+1)
        exit()
    else:
        print(v[0]-L[-1]+1)
        exit()
else:
    print('0')

# Fixed code:


v = [int(d) for d in input().split()]

LM = []
for d in range(v[1]):
   LM.append([int(d) for d in input().split()])

L = [d[0] for d in LM]
M = [d[1] for d in LM]
L.sort()
M.sort()
if L[-1] <= M[0]:
    if M[0] <= v[0]:
        print(M[0]-L[-1]+1)
        exit()
    else:
        print(v[0]-L[-1]+1)
        exit()
else:
    print('0')
# Buggy code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: map(str, sys.stdin.buffer.readline().split())
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n, m = rm()
l = 0
r = inf
for _ in range():
  a, b = rm()
  a = max(a, l)
  b = min(b, inf)
print(b-a+1)



# Fixed code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: map(str, sys.stdin.buffer.readline().split())
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n, m = rm()
l = 0
r = inf
for _ in range(m):
  a, b = rm()
  l = max(a, l)
  r = min(b, r)
c = r-l+1
if c >= 1:
  print(c)
else:
  print(0)
# Buggy code:
def resolve():
    (n, m) = list(map(int, input().split(" ")))
    
    for i in range(m):
        o = list(map(int, input().split(" ")))
        if i == 0:
            lmax = o[0]
            rmin = o[1]
        else:
            lmax = max(lmax, o[0])
            rmin = min(rmin, o[1])

    print(rmin-lmax+1 if rmin > lmax else 0)


if '__main__' == __name__:
    resolve()

# Fixed code:
def resolve():
    (n, m) = list(map(int, input().split(" ")))
    
    for i in range(m):
        o = list(map(int, input().split(" ")))
        if i == 0:
            lmax = o[0]
            rmin = o[1]
        else:
            lmax = max(lmax, o[0])
            rmin = min(rmin, o[1])

    print(rmin-lmax+1 if lmax <= rmin else 0)


if '__main__' == __name__:
    resolve()
# Buggy code:
n, m = map(int, input().split())
_min = 0
_max = n
for _ in range(m):
  l, r = map(int, input().split())
  _min = min(_min, r)
  _max = max(_max, l)
  print(max(_max - _min + 1, 0))

# Fixed code:
def p_c():
    n, m = map(int, input().split())
    _min = n
    _max = 0
    for _ in range(m):
        l, r = map(int, input().split())
        _min = min(_min, r)
        _max = max(_max, l)
    print(max(_min - _max + 1, 0))

p_c()
# Buggy code:
n,m = map(int,input().split())

Lmax = 0
Rmin = 100000
for i in range(m):
	L,R = map(int,input().split())
	if Lmax < L:
		Lmax = L
	if Rmin > R:
		Rmin = R
if Rmin > Lmax:
	print("0")
else:
	print("%d" % (Rmin - Lmax +1))

# Fixed code:
n,m = map(int,input().split())

Lmax = 0
Rmin = 1000000
for i in range(m):
	L,R = map(int,input().split())
	if Lmax < L:
		Lmax = L
	if Rmin > R:
		Rmin = R
res = Rmin - Lmax + 1
if res > 0:
	print("%d" % (Rmin - Lmax +1))
else:
	print(0)
# Buggy code:
from sys import stdin

n, m = (int(x) for x in stdin.readline().rstrip().split())
lr = [stdin.readline().rstrip().split() for _ in range(m)]
l = max([i[0] for i in lr])
r = min([i[1] for i in lr])

if l <= r:
    print(r-l+1)
else:
    print(0)


# Fixed code:
from sys import stdin

n, m = (int(x) for x in stdin.readline().rstrip().split())
lr = [stdin.readline().rstrip().split() for _ in range(m)]
l = max([int(i[0]) for i in lr])
r = min([int(i[1]) for i in lr])

if l <= r:
    print(r-l+1)
else:
    print(0)
# Buggy code:
N,K=(int(x) for x in input().split())
S=list(int(i) for i in input())
S[K-1] = S[K-1].lower()
print(','.join(S))

# Fixed code:
N,K=(int(x) for x in input().split())
S=list(input())
S[K-1] = S[K-1].lower()
print(''.join(S))