Problem:


Score : 300 points

Problem Statement

We have N ID cards, and there are M gates.

We can pass the i-th gate if we have one of the following ID cards: the L_i-th, (L_i+1)-th, ..., and R_i-th ID cards.

How many of the ID cards allow us to pass all the gates alone?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i \leq R_i \leq N

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

Print the number of ID cards that allow us to pass all the gates alone.

Sample Input 1

4 2
1 3
2 4

Sample Output 1

2

Two ID cards allow us to pass all the gates alone, as follows:

The first ID card does not allow us to pass the second gate.

The second ID card allows us to pass all the gates.

The third ID card allows us to pass all the gates.

The fourth ID card does not allow us to pass the first gate.

Sample Input 2

10 3
3 6
5 7
6 9

Sample Output 2

1

Sample Input 3

100000 1
1 100000

Sample Output 3

100000



Sample input:
4 2
1 3
2 4


Sample output:
2

Submission status: Wrong Answer

Solution code:
N, M = map(int,input().split())
LRs = [list(map(int,input().split())) for _ in range(M)]

current_L = 1
current_R = N
for L,R in LRs:
    if current_L < L:
        current_L = L
    if current_R > R:
        current_R = R


if current_R < current_L:
    print(0)
print(current_R-current_L+1)

Submission status: Wrong Answer

Solution code:
N, M = map(int,input().split())
LRs = [list(map(int,input().split())) for _ in range(M)]

current_L = 1
current_R = N
for L,R in LRs:
    if current_L < L:
        current_L = L
    if current_R > R:
        current_R = R


print(current_R-current_L+1)

Submission status: Wrong Answer

Solution code:
N, M = map(int,input().split())
LRs = [list(map(int,input().split())) for _ in range(M)]

current_L = 0
current_R = N
for L,R in LRs:
    if current_L < L:
        current_L = L
    if current_R > R:
        current_R = R


print(current_R-current_L+1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n, m = map(int, input().split())
min_c = 1
max_c = n
for i in range(m):
    l, r = map(int, input().split())
    if l > min_c:
        min_c = l
    if r < max_c:
        max_c = r
if max_c > min_c:
    print(max_c-min_c+1)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n, m = map(int, input().split())
min_c = 1
max_c = n
for i in range(m):
    l, r = map(int, input().split())
    if l > min_c:
        min_c = l
    if r < max_c:
        max_c = r
print(max_c-min_c+1)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
lr = []
for i in range(M):
    lr.append(list(map(int, input().split())))

l = []
for i in range(M):
    l.append(lr[i][0])
L = max(l)

r = []
for i in range(M):
    r.append(lr[i][1])
R = min(r)

if L>=R:
    print(0)
else:
    print(R-L+1)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
lr = []
for i in range(M):
    lr.append(list(map(int, input().split())))

l = []
for i in range(M):
    l.append(lr[i][0])
L = max(l)

r = []
for i in range(M):
    r.append(lr[i][1])
R = min(r)

print(R-L+1)

Submission status: Wrong Answer

Solution code:
n, m = list(map(int, input().split()))
maxl = 0
minr = 10000
for i in range(m):
    l, r = list(map(int, input().split()))
    maxl = max(maxl, l)
    minr = min(minr, r)
if minr > maxl:
    print(0)
else:
    print(maxl - minr + 1)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())

X = []
for i in range(M):
    X.append([int(j) for j in input().split()])

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

print(Rmin-Lmax+1)

Submission status: Wrong Answer

Solution code:
def main(): 


    card_cnt, gate_cnt = map(int, input().split())

    LR = set(range(1, 10000+1))

    for _ in range(gate_cnt):
        L, R = map(int, input().split())
        LR.intersection_update(range(L, R+1))

    print(len(LR))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N,M = (int(i) for i in input().split())
min = 1
max = N

for i in range(M):
    L = list(map(int,input().split()))
    if L[0] > min:
        min = L[0]
    if L[1] < max:
        max = L[1]

print(max-min+1)

Submission status: Wrong Answer

Solution code:
N,M = (int(i) for i in input().split())
min = 0
max = N

for i in range(M):
    L = list(map(int,input().split()))
    if L[0] > min:
        min = L[0]
    if L[1] < max:
        max = L[1]

print(max-min+1)

Submission status: Wrong Answer

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

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

print(minr-maxl+1)

Submission status: Wrong Answer

Solution code:
nm=list(map(int,input().split()))
low=1
high=nm[0]
for i in range(nm[1]):
    lr=list(map(int,input().split()))
    low=max([low,lr[0]])
    high=min([high,lr[1]])
print(high-low+1)

Submission status: Wrong Answer

Solution code:
N, M=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)

Submission status: Wrong Answer

Solution code:
N, M=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)
print(minR-maxL+1)

Submission status: Wrong Answer

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
##############################

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

if L <= R:
    print(R-L+1)
else:
    print(0)

Submission status: Wrong Answer

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
##############################

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

print(R-L+1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
R = []
K = [0]*N
cnt = 0
ans = 0
for i in range(M):
    L = list(map(int, input().split()))
    R.append(L)
#print(R)
for i in range(M):
    for j in range(N):
        if R[i][0]<=j<=R[i][1]:
            K[j-1] += 1
#            print(K)
for j in range(N-1):
    if K[j]==M:
        ans +=1
print(ans)

Submission status: Wrong Answer

Solution 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(0 if min(R)>max(L) else min(R)-max(L)+1)

Submission status: Wrong Answer

Solution 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(min(R)-max(L)+1)

Submission status: Wrong Answer

Solution code:
n,m=map(int, input().split())
l=[0]*m
r=[0]*m
for i in range(m):
    x,y=map(int, input().split())
    l[i]=x
    r[i]=y
x=max(l)
y=min(r)
if x>n:
    print(0)
else:
    print(min(y-x+1,n-x+1))

Submission status: Wrong Answer

Solution code:
N, M = map(int,input().split())
lr = [tuple(map(int,input().split())) for _ in range(M)]
ls = []
rs = []
for i in range(M):
  ls.append(lr[i][0])
  rs.append(lr[i][1])

print(min(rs) - max(ls) + 1)

Submission status: Wrong Answer

Solution code:
n, m = list(map(int, input().split()))
l = []
r = []
for i in range(m):
    x, y=[int(i) for i in input().split()]
    l.append(x)
    r.append(y)
ans = 0
for i in range(n):
    cnt = 0
    for j in range(m):

        for k in range(l[j], r[j]+1):
            print(l[j], end=' ')
            print(r[j])
            if i + 1 == k:
                cnt += 1
        if cnt == m:
            ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n, m = list(map(int, input().split()))
l = []
r = []
for i in range(m):
    x, y=[int(i) for i in input().split()]
    l.append(x)
    r.append(y)
ans = 0
for i in range(n):
    cnt = 0
    for j in range(m):

        for k in range(l[j], r[j]+1):
            print(l[j], end=' ')
            print(r[j])
            if i + 1 == k:
                cnt += 1
                print('cnt='+ str(cnt))
        if cnt == m:
            ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
#C - Prison(ABC127)
N,M=map(int, input().split())
l,r=map(int, input().split())
for i in range(M-1):
    L,R=map(int,input().split())
    l=max(L,l)
    r=min(r,R)
print(r-l+1)

Submission status: Wrong Answer

Solution code:
#C - Prison(ABC127)
N,M=map(int, input().split())
l,r=map(int, input().split())
for i in range(M-1):
    L,R=map(int,input().split())
    if L>l:
        l=L
    if R<r:
        r=R
print(r-l+1)

Submission status: Wrong Answer

Solution code:
while True:
    try:
        n, m = map(int, input().split())
        start = 1
        end = n
        for i in range(m):
            a, b = map(int, input().split())
            if a > end or b < start:
                print(0)
                for j in range(m-i-1):
                    input()
                break
            start = max(start, a)
            end = min(end, b)   
        print(end-start+1)
    except:
        break

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
tl, tr = 0, float("inf")
for _ in range(m):
    l, r = map(int, input().split())
    if tr>=l or tl<=r:
        tl = l if tl<l else tl
        tr = r if tr>r else tr
    else:
        tl, tr = 1, 0; break
print(tr-tl+1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
tl, tr = 0, float("inf")
for _ in range(m):
    l, r = map(int, input().split())
    tl = l if tl<l else tl
    tr = r if tr>r else tr
print(tr-tl+1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split(" "))

s, e = 1, n
for i in range(m):
    l, r = map(int, input().split(" "))
    if s < l: s = l
    if e > r: e = r

print(e-s+1)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
LR=[list(map(int,input().split())) for _ in range(m)]

L=[i[0] for i in LR]
R=[i[1] for i in LR]
print(min(R)-max(L)+1)

Submission status: Wrong Answer

Solution code:
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()))
    
min_temp = L[0]
max_temp = R[0]
ans = R[0] - L[0] + 1

for i in range(1, M):
    
    if min_temp < L[i]:
        ans -= (L[i] - min_temp)
        min_temp = L[i]
    
    if R[i] < max_temp:
        ans -= (max_temp - R[i])
        max_temp = R[i]
        
print(ans)

Submission status: Wrong Answer

Solution code:
while True:
    try:
        N, M = map(int, input().split())
        gate = [list(map(int, input().split())) for i in range(M)]
        
        L, R = 0, 10**5+5
        flag = True
        for l, r in gate:
            L = max(L, l)
            if r < L:
                flag = False
                break
            R = min(R, r)
        
        if flag:
            print(R-L+1)
        else:
            print(0)
    except:
        break

Submission status: Wrong Answer

Solution code:
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(max(0, L - R + 1))

Submission status: Wrong Answer

Solution code:
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(L - R + 1)

Submission status: Wrong Answer

Solution code:
while True:
    try:
        n, m = map(int, input().split())
        start = 1
        end = n
        for i in range(m):
            a, b = map(int, input().split())
            if a > end or b < start or b < a:
                print(0)
                break
            start = max(start, a)
            end = min(end, b)
        else:
            print(end-start+1)
    except:
        break

Submission status: Wrong Answer

Solution code:
while True:
    try:
        n, m = map(int, input().split())
        start = 1
        end = n
        for i in range(m):
            a, b = map(int, input().split())
            if a > end or b < start:
                print(0)
                break
            start = max(start, a)
            end = min(end, b)
        else:
            print(end-start+1)
    except:
        break

Submission status: Wrong Answer

Solution 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 = max(L_list)
R_min = min(R_list)

count = 0
for i in range(M):
    if L_max >= L_list[i] and R_min <= R_list[i]:
        count += 1
print(count)

Submission status: Wrong Answer

Solution code:
n,m = [int(i) for i in input().split()]

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

Submission status: Wrong Answer

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

print(min(r) - max(l) + 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
low, high = map(int, input().split())

for i in range(m-1):
    l, r = map(int, input().split())
    if low <= l <= high :
        low = max(l,low)
    if low <= r <= high :
        high = min(r,high)

print(high-low+1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
low, high = map(int, input().split())

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

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
low, high = map(int, input().split())

for i in range(m-1):
    l, r = map(int, input().split())
    low = max(l,low)
    high = min(r,high)
print(high-low+1)

Submission status: Wrong Answer

Solution code:
a,b= map(int,input().split())
x, y= zip(*[list(map(int,input().split())) for i in range(b)])
print(min(y) - max(x) +1)

Submission status: Wrong Answer

Solution code:
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(R-L+1)

Submission status: Wrong Answer

Solution code:
n, m = 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)
print(minr - maxl + 1)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
l = [list(map(int,input().split())) for i in range(M)]


inter_l = set([i for i in range(1,N+1)])

for i in range(len(l)):
 if i < len(l)-1:
  min_l = l[i][0]
  max_l = l[i][-1]
  min_next_l = l[i+1][0]
  max_next_l = l[i+1][1]
  l_arr=set([i for i in range(min_l,max_l+1)])
  next_l_arr=set([i for i in range(min_next_l,max_next_l+1)])
  inter_l = next_l_arr & l_arr & inter_l

print(len(inter_l))

Submission status: Wrong Answer

Solution code:
while True:
    try:
        N, M = map(int, input().split())
        gate = [list(map(int, input().split())) for i in range(M)]
        
        dp = [0] * (N+1)
        for l, r in gate:
            dp[l-1] += 1
            dp[r] -= 1
            print(dp)
        
        cnt = 0
        ans = 0
        for i in range(N):
            cnt += dp[i]
            if cnt == M:
                ans += 1
            
        print(ans)
    except:
        break

Submission status: Wrong Answer

Solution code:
while True:
    try:
        N, M = map(int, input().split())
        gate = [list(map(int, input().split())) for i in range(M)]
        
        L, R = 0, 10**5+5
        for l, r in gate:
            L = max(L, l)
            R = min(R, r)
            
        print(R-L+1)
    except:
        break

Submission status: Wrong Answer

Solution code:
n,m=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(r-l+1)

Submission status: Wrong Answer

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    lr = [list(map(int, input().rstrip('\n').split())) for _ in range(m)]
    a = [0] * (n + 2)
    for l, r in lr:
        a[l] += 1
        a[r + 1] += 1
    t = 0
    cnt = 0
    for i in range(1, n + 1):
        t += a[i]
        if t == m:
            cnt += 1
    print(cnt)


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
max_L = 1
min_R = 100000
for _ in range(M):
	L, R = map(int, input().split())
	if max_L < L:
		max_L = L
	if R < min_R:
		min_R = R
print(min_R - max_L + 1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
max_L = 0
min_R = 100000
for _ in range(M):
	L, R = map(int, input().split())
	if max_L < L:
		max_L = L
	if R < min_R:
		min_R = R
print(min_R - max_L + 1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
L_R = [[int(i) for i in input().split()] for _ in range(M)]

max_R, max_L = L_R[M - 1 - 1][1], L_R[M - 1][0]
cnt = 0

for i in range(M - 2, 0, -1):
    if max_R < max_L:
        cnt = -1
        break
    max_R, max_L = L_R[i - 1][1], max_L

print("{}".format(0 if cnt == -1 else max_R - max_L + 1))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split(" "))
card = {i+1 for i in range(n)}

x,y = map(int,input().split(" "))
for i in range(m-1):
    l,r = map(int,input().split(" "))
    x = max(x,l)
    r = min(y,r)
if y-x >= 0:
    print(y-x+1)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
def add(n):
    return False

N,M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]
L1 = []
R1 = []
for i in range(M):
    L1.append(LR[i][0])
    R1.append(LR[i][1])
if max(R1)>=max(L1):
    print(min(R1)-max(L1)+1)
else:
    print(0)

Submission status: Wrong Answer

Solution 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)

ans_min,ans_max=L[0],R[0]

def compare(min_,max_,min_i,max_i):
  if max_<min_i:
    ret_min,ret_max = -1,-1
  elif min_>max_i:
    ret_min,ret_max = -1,-1
  elif (min_<=min_i) and (min_i<=max_) and (max_<=max_i):
    ret_min,ret_max = min_i,max_
  elif (min_>=min_i) and (max_<=max_i):
    ret_min,ret_max = min_,max_
  elif (min_>=min_i) and (min_<=max_i) and (max_>=max_i):
    ret_min,ret_max = min_,max_i
  elif (min_<=min_i) and (max_i<=max_):
    ret_min,ret_max = min_i,max_i
  else:
    pass
#    print("else!")
  return ret_min,ret_max

for i in range(1,M):
  ans_min,ans_max = compare(ans_min,ans_max,L[i],R[i])
  if ans_min==-1 and ans_max==-1:
    break
#print("End",ans_min,ans_max)
if ans_min<=N:
  if ans_max<=N:
    print(ans_max-ans_min+1)
  else:
    print(N-ans_min+1)
else:
  print(0)

Submission status: Wrong Answer

Solution code:
N,M = map(int, input().split())

set_cnt = set(list(map(int,input().split(" "))))
min_set_cnt = min(set_cnt)
max_set_cnt = max(set_cnt)


for m in range(M-1):
  _first, _last = map(int,input().split(" "))
  min_set_cnt = max(min_set_cnt,_first)
  max_set_cnt = min(max_set_cnt,_last)


print(max_set_cnt - min_set_cnt + 1)

Submission status: Wrong Answer

Solution code:
def main():
    N, M = map(int, input().split())
    
    left = 0
    right = float('inf')
    
    for i in range(M):
        L, R = map(int, input().split())
        left = max(left, L)
        right = min(right, R)
    
    
    print(right - left + 1)


main()

Submission status: Wrong Answer

Solution code:
def main():
    N, M = map(int, input().split())
    
    left = 0
    right = float('inf')
    
    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)


main()

Submission status: Wrong Answer

Solution code:
import sys
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(R[0]-L[M-1]+1)

Submission status: Wrong Answer

Solution code:
import sys
N, M = map(int, input().split())
L, R = map(int, input().split())
if M == 1:
  print(R-L+1)
  sys.exit()
for i in range(M-1):
  l, r = map(int, input().split())
print(R-l+1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
L = [0] * M
R = [0] * M
L, R = map(int, input().split())
ans = set(list(range(L, R+1)))
for i in range(M-1):
  L, R = map(int, input().split())
  ans = ans & set(list(range(L, R+1)))
  if len(ans) == 0:
    print(0)
print(len(ans))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split(" "))
card = {i+1 for i in range(n)}

x,y = map(int,input().split(" "))
for i in range(m-1):
    l,r = map(int,input().split(" "))
    if l > y or r < x:
        x = 1
        y = 0
        break;
    elif x <= r and r < y:
        y = r
    elif l <= y and x < l:
        x = l
    elif l < x and y < r:
        continue
    else:
        x = l
        y = r
print(y-x+1)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split(" "))
card = {i+1 for i in range(n)}

x,y = map(int,input().split(" "))
for i in range(m-1):
    l,r = map(int,input().split(" "))
    if l > y or r < x:
        x = 1
        y = 0
        break;
    elif x <= r and r < y:
        y = r
    elif l <= y and x < r:
        x = l
    elif l < x and y < r:
        continue
    else:
        x = l
        y = r
print(y-x+1)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split(" "))
card = {i+1 for i in range(n)}

x,y = map(int,input().split(" "))
for i in range(m-1):
    l,r = map(int,input().split(" "))
    if l > y or r < x:
        x = 1
        y = 0
        break;
    elif x <= r and r < y:
        y = r
    else:
        x = l
print(y-x+1)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split(" "))
card = {i+1 for i in range(n)}

x,y = map(int,input().split(" "))
for i in range(m-1):
    l,r = map(int,input().split(" "))
    if l > y or r < x:
        x = 0
        y = 0
        break;
    elif x <= r and r < y:
        y = r
    else:
        x = l
print(y-x+1)

Submission status: Wrong Answer

Solution code:
N, M = list(map(int, input().split()))
ID = list(range(1, N+1))
for i in range(M):
    L, R = list(map(int, input().split()))
    r = list(range(L, R+1))
    ID = set(ID) & set(r)
    if len(ID) > N:
        print(N)
        exit()
    elif len(ID) == 0:
        print(0)
print(len(ID))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
low = 1
high = 1e+5
for i in range(M):
    L, R = map(int, input().split())
    if low < L:
        low = L 
    if R < high:
        high = R
    if low > high:
        break
print(int(high-low+1))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
keys = []
for i in range(M):
  keys.append(list(map(int, input().split())))
  
passkeys = [1, N]
for i in range(M):
  passkeys[0] = max(passkeys[0], keys[i][0])
  passkeys[1] = min(passkeys[1], keys[i][1])
  
print(passkeys[1] - passkeys[0] + 1)

Submission status: Wrong Answer

Solution code:
import heapq
n, m = map(int, input().split())
lr = []
heap1 = []
heap2 = []
for i in range(m):
    lr.append(list(map(int, input().split())))
for j in range(m):
    heapq.heappush(heap1, -lr[j][0])
    heapq.heappush(heap2, lr[j][1])
print(heapq.heappop(heap2) + heapq.heappop(heap1) +1)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    print(min(R)-max(L)+1)
    return


# Generated by 1.1.5 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    L = [int()] * (M)  # type: "List[int]"
    R = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))
    solve(N, M, L, R)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
l_list,r_list=[],[]
for _ in range(m):
  l,r=map(int,input().split())
  l_list.append(l)
  r_list.append(r)
print(min(r_list)-max(l_list)+1)

Submission status: Wrong Answer

Solution code:
def intersect(a, b, c, d):
    if b < c or d < a:
        return -2, -1
    else:
        li = sorted([a, b, c, d])
        return li[1], li[2]
        
N, M = map(int, input().split())
LR = []
for _ in range(M):
    LR.append(tuple(map(int, input().split())))
    
a, b = LR[0]
for lr in LR:
    a, b = intersect(a, b, *lr)
    
ans = b-a+1
print(ans) if ans > 0 else print('0')

Submission status: Wrong Answer

Solution code:
def intersect(a, b, c, d):
    if b <= c or d <= a:
        return -2, -1
    else:
        li = sorted([a, b, c, d])
        return li[1], li[2]
        
N, M = map(int, input().split())
LR = []
for _ in range(M):
    LR.append(tuple(map(int, input().split())))
    
a, b = LR[0]
for lr in LR:
    a, b = intersect(a, b, *lr)
    
print(b-a + 1)

Submission status: Wrong Answer

Solution code:
def intersect(a, b, c, d):
    if b <= c or a <= d:
        li = sorted([a, b, c, d])
        return li[1], li[2]
    else:
        return -2, -1
        
N, M = map(int, input().split())
LR = []
for _ in range(M):
    LR.append(tuple(map(int, input().split())))
    
a, b = LR[0]
for lr in LR:
    a, b = intersect(a, b, *lr)
    
print(b-a + 1)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
src = [tuple(map(int,input().split())) for i in range(m)]

l_max,r_min = 0,10**5
for l,r in src:
    l_max = max(l_max,l)
    r_min = min(r_min,r)
else:
    print(r_min-l_max+1)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
D=[list(map(int,input().split())) for i in range(m)]
maxI=n
minI=1
for i in range(m):
  maxI=max(maxI,D[i][1])
  minI=min(minI,D[i][0])
print(maxI-minI+1)

Submission status: Wrong Answer

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

min_i = 0
max_i = 100000

for m in range(M):
    min_i = max(min_i, LR[m][0])
    max_i = min(max_i, LR[m][1])

print(max_i - min_i + 1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
min_value = 0
max_value = N  
for i in range(M):
    l, r = list(map(int, input().split()))
    if min_value < l:
        min_value = l
    if max_value > r:
        max_value = r
print(max_value-min_value+1)

Submission status: Wrong Answer

Solution code:
N,M = map(int, input().split())
for i in range(M):
    if i == 0 :
        L,R = map(int, input().split())
    else:
        l,r = map(int, input().split())
        L = max(L,l)
        R = min(R, r)
print(R - L + 1)

Submission status: Wrong Answer

Solution 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(r-l+1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())

left = 0
right = N
for i in range(M):
    L, R = map(int, input().split())
    left = max(left, L)
    right = min(right, R)

print(right - left + 1)

Submission status: Wrong Answer

Solution code:
import numpy as np
N, M = map(int, input().split())
LR = np.array([list(map(int, input().split())) for _ in range(M)])
lmax, rmin = LR.max(axis=0)[0], LR.min(axis=0)[1]
print(rmin-lmax+1)

Submission status: Wrong Answer

Solution code:
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)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
ans = []
for _ in range(m):
    l,r = map(int,input().split())
    if ans:
        ans = [max(min(ans),l),min(max(ans),r)]
    else:
        ans = [l,r]
print(ans[1]-ans[0]+1 if ans[1]-ans[0]+1>0 else 0)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
ans = []
for _ in range(m):
    l,r = map(int,input().split())
    if ans:
        ans = [max(min(ans),l),min(max(ans),r)]
    else:
        ans = [l,r]
c = 0
for i in range(ans[0],ans[1]+1):
    c += 1
print(c)

Submission status: Wrong Answer

Solution code:
import math
import copy
import fractions
import numpy as np

# ===FUNCTION===

def getInputNum():
    inputNum = int(input())
    return inputNum

def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList

def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList

# ===CODE===

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

data = getSomeInputListInt(m)

lower = []
upper = []

for i in range(m):
    lower.append(data[i][0])
    upper.append(data[i][1])

lowerNum = max(lower)
upperNum = min(upper)

ans = upperNum - lowerNum +1

print(ans)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
L, R = [0] * M, [0] * M
Lmax, Rmin = 0, 1e10
for i in range(M):
    L, R = map(int, input().split())
    if L > Lmax:
        Lmax = L
    if R < Rmin:
        Rmin = R

print(Lmax, Rmin)
print(max(Rmin - Lmax + 1, 0))

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
min_num = 0
max_num = 10**5
           
for i in range(m):
  min_temp, max_temp = map(int, input().split())
  if min_temp > min_num:
    min_num = min_temp
  if max_temp < max_num:
    max_num = max_temp
print(max_num - min_num + 1)

Submission status: Wrong Answer

Solution code:
n = list(map(int,input().split()))
l = [list(map(int,input().split())) for i in range(n[1])]
minp = l[0][0]
maxp = l[0][1]
for t in range(n[1]):
    minp = max(minp,l[t][0])
    maxp = min(maxp,l[t][1])

print(max(0,minp-maxp)+1)

Submission status: Wrong Answer

Solution code:
n = list(map(int,input().split()))
l = [list(map(int,input().split())) for i in range(n[1])]

passcard = []
for c in range(l[0][1] - l[0][0]+1):
    passcard.append(l[0][0]+c)

for t in range(n[1],0):
    for i in range(len(passcard)):
        if not(l[t][0] <= passcard[i] <= l[t][1]):
            passcard[i] = 0
        else:
            passcard[i] = passcard[i]
    passcard = [i for i in passcard if i!=0]
            
out = [i for i in passcard if i!=0]
print(len(out))

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
maxi = 10**5
mini = 0
for _ in range(m):
    s,t = map(int,input().split())
    maxi = min(maxi,t)
    mini = max(mini,s)
print(maxi - mini +1)