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:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, M = MAP()
ID = [LIST() for _ in range(M)]
ID_t = [list(x) for x in zip(*ID)]

print(min(ID_t[1]) - max(ID_t[0]) + 1)

Submission status: Wrong Answer

Solution code:
import sys
import os


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, M = list(map(int, sys.stdin.buffer.readline().split()))
    mi = 1
    mx = N

    for m in range(M):
        L, R = map(int, sys.stdin.buffer.readline().split())
        mi = max(mi, L)
        mx = min(mx, R)

    print(mx - mi + 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import os


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, M = list(map(int, sys.stdin.buffer.readline().split()))
    LR = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]
    mi = 1
    mx = N

    for lr in LR:
        l = lr[0]
        r = lr[1]
        mi = max(mi, l)
        mx = min(mx, r)

    print(mx - mi + 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import os


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, M = list(map(int, sys.stdin.buffer.readline().split()))
    LR = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]
    mi = 0
    mx = 10 ** 5 + 1

    for lr in LR:
        l = lr[0]
        r = lr[1]
        mi = max(mi, l)
        mx = min(mx, r)

    print(mx - mi + 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import os


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, M = list(map(int, sys.stdin.buffer.readline().split()))
    LR = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]
    mi = 0
    mx = 10 ** 5 + 1

    for lr in LR:
        l = lr[0]
        r = lr[1]
        mi = max(mi, l)
        mx = min(mx, r)
        print(mi, mx)

    print(mx - mi + 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

for _ in range(M):
    L_i,R_i = map(int,input().split())
    if L_i > min:
        min = L_i
    if R_i < max:
        max = R_i

if min > max:
    print(0)
else:
    print(max - max + 1)

Submission status: Wrong Answer

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

for _ in range(M):
    L_i,R_i = map(int,input().split())
    if L_i > min:
        min = L_i
    if R_i < max:
        max = R_i

if min > max:
    print("0")
else:
    print(max - max + 1)

Submission status: Wrong Answer

Solution code:
A = list(map(int , input().split()))
sc = [list(map(int, input().split())) for i in range(A[1])]
a = []
b = []
for i in sc:
    a.append(i[0])
    b.append(i[1])
if max(a) < min(b):
    ans = min(b) - max(a) + 1
else:
    ans = 0
print(ans)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
mini, maxi = 0, 10**5+1

for _ in range(m):
    l, r = map(int, input().split())
    if l>mini:
        mini = l
    if r<maxi:
        maxi = r

print(maxi - mini + 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:
n, m = map(int, input().split())
L = 1
R = n
for i in range(m):
    l, r = map(int, input().split())
    L = max(L, l)
    R = min(R, r)
print(R - L + 1)

Submission status: Wrong Answer

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

left = 1
right = n
for i in range(m):
    l, r = list(map(int, input().split()))
    left = max(l, left)
    right = min(r, right)

print(right - left + 1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
l, r = 1, 10 ** 5
for _ 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())
mn = 1
mx = n

for i in range(m):
    l,r = map(int,input().split())
    if l > mn:
        mn = l
    if r < mx:
        mx = r
        
if mx > mn:
    print(0)
else:
    print(mn - mx + 1)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
mn = 10**9
mx = -1

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

print(abs(mx-mn)+1)

Submission status: Wrong Answer

Solution code:
import numpy as np
N,M = map(int,input().split(" "))
Min = 1
Max = N

for _ in range(M):
    L,R = map(int,input().split(" "))
    if L>Min:
        Min=L
    if R<Max:
        Max=R
print(Max-Min+1)

Submission status: Wrong Answer

Solution code:
n = list(map(int, input().split()))
N = n[0]
M = n[1]
X = []
Y = []
for i in range(M):
    a = list(map(int, input().split()))
    X.append(a[0])
    Y.append(a[1])
v=0
z=[X[0],Y[0]]
while v==0:
    for i in range(1,M):
        if z[0]<=Y[i] and z[1]>=X[i]:
            z[0]=max(z[0],X[i])
            z[1]=min(z[1],Y[i])
        else:
            v=1
        if i==M-1:
            v=1
print(z[1]-z[0]+1)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
ans = [-1,10**6]

for i in range(m):
    tmp = list(map(int,input().split()))
    ans[0] = max(ans[0],min(tmp))
    ans[1] = min(ans[1],max(tmp))

print(ans[1]-ans[0]+1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split()) 
left = 1
right = N
for _ in range(M):
    L, R = map(int, input().split())
    if left < L:
        left = L
    if R < right:
        right = R
print(right - left + 1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
card = False
for _ in range(M):
    L, R = map(int, input().split())
    if card is True:
        card = card & set([i for i in range(L, R + 1)])
    else:
        card = set([i for i in range(L, R + 1)])
print(len(card))

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
z = []
for i in range(m):
    x, y = map(int, input().split())
    z.append({a for a in range(x, y+1)})
print(z)
ans = {a for a in range(1, n+1)}
for i in z:
    ans &= i
print(len(ans))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
#C - Prison
N,M = map(int, input().split())
L = [input().split() for l in range(M)]

a = max(L, key = lambda x:x[0])
b = min(L, key = lambda x:x[1])
ans = int(b[1]) - int(a[0])+1
if ans >= 0:
    print(ans)
else:
    print(0)

Submission status: Wrong Answer

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

min_num=0
max_num=10**5
for l,r in lr:
    if l>min_num:min_num=l
    if r<max_num:max_num=r

print(max_num-min_num+1)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
lists = []
L = []
R = []
for i in range(m):
    l,r = map(int,input().split())
    L.append(l)
    R.append(r)
ll = L[0]
rr = R[0]
ans = []
for i in range(m):
    if L[i] <= rr and L[i] >= ll:
        ans.append(L[i])
        ll = L[i]
ans_list = list(range(ll,rr+1))
print(len(ans_list))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
lists = []
L = []
R = []
for i in range(m):
    l,r = map(int,input().split())
    if m == 1:
        print(r-l+1)
        exit()
    L.append(l)
    R.append(r)
ll = L[0]
rr = R[0]
ans = []
for i in range(m):
    if L[i] <= rr and L[i] >= ll:
        ans.append(L[i])
        ll = L[i]
ans_list = list(range(ll,rr+1))
print(len(ans_list))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n, m = map(int,input().split())
lr = []
for i in range(m):
    lr.append([int(i) for i in input().split()])

if m == 1:
    print(lr[0][1] - lr[0][0] + 1)
    exit()

tmp1 = 0
tmp2 = pow(10, 6)
for i in range(m - 1):
    tmp1 = max(lr[i][0], lr[i + 1][0], tmp1)
    tmp2 = min(lr[i][1], lr[i + 1][1], tmp2)
#print(tmp1, tmp2)
ans = tmp2 - tmp1 + 1
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n, m = map(int,input().split())
lr = []
for i in range(m):
    lr.append([int(i) for i in input().split()])

if m == 1:
    print(lr[0][1] - lr[0][0] + 1)
    exit()

tmp1 = 1
tmp2 = pow(10, 5) + 1
for i in range(m - 1):
    tmp1 = max(lr[i][0], lr[i + 1][0], tmp1)
    tmp2 = min(lr[i][1], lr[i + 1][1], tmp2)
#print(tmp1, tmp2)
ans = tmp2 - tmp1 + 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

n, m = MAP()
ml = -1
mr = n
for _ in range(m):
    l, r = MAP()
    ml = max(ml, l)
    mr = min(mr, r)
print(mr - ml + 1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
L = []
R = []
for _ in range(M):
  l, r = map(int, input().split())
  L.append(l)
  R.append(r)

l = max(L)
r = min(R)
print(r-l+1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
l, r = 1, N
for _ in range(M):
    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 = 0; r = N;
for i in range(M):
  a, b = map(int,input().split())
  l = max(l, a)
  r = min(r, b)
print(max(0, abs(r-l)+1))

Submission status: Wrong Answer

Solution code:
n, m = map(int,input().split())
l = [0]*m
r = [0]*m
for i in range(m):
    l1,r1 = map(int,input().split())
    l[i] = l1
    r[i] = r1
print(r[0] - l[-1] + 1)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
ml,mr=map(int,input().split())
for i in range(m-1):
    l,r=map(int,input().split())
    if l>ml:
        ml=l
    if r<mr:
        mr=r
print(mr-ml+1)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
ml,mr=map(int,input().split())
for i in range(m-1):
    l,r=map(int,input().split())
    if l>=ml:
        ml=l
    if r<=mr:
        mr=r
print(mr-ml+1)

Submission status: Wrong Answer

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

ans = [i+1 for i in range(n)]
for i in range(m):
  l, r = map(int, input().split())
  for j in ans:
    if l > j or j > r:
      ans.remove(j)
    
print(len(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(min(R) - max(L) + 1)

Submission status: Wrong Answer

Solution code:

n, m = map(int, input().split())
L = [0] * m
R = [0] * m
judge = [0] * n
for i in range(m):
    L[i], R[i] = map(int, input().split())
    for j in range(L[i], R[i] + 1):
        judge[j - 1] += 1

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

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
left = [None] * M
right = [None] * M
for i in range(M):
    left[i], right[i] = map(int, input().split())

ans = min(right) - max(left) + 1
if ans < 0:
    print(ans)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
gate = [None] * M
range_L = 1
range_R = N
for i in range(M):
    gate[i] = tuple(map(int, input().split()))
    left = gate[i][0]
    right = gate[i][1]
    if range_L < left:
        range_L = left
    if right < range_R:
        range_R = right

ans = range_R - range_L + 1
if ans < 0:
    print(ans)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
gate = [None] * M
range_L = 1
range_R = N
for i in range(M):
    gate[i] = tuple(map(int, input().split()))
    left = gate[i][0]
    right = gate[i][1]
    if range_L < left:
        range_L = left
    if right < range_R:
        range_R = right

print(range_R - range_L + 1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
gate = [None] * M
range_L = 1
range_R = N
for i in range(M):
    gate[i] = tuple(map(int, input().split()))
    left = gate[i][0]
    right = gate[i][1]
    if range_L < left:
        range_L = left
    if right < range_R:
        range_R = right

print(range_R, range_L)
print(range_R - range_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())
  
count = 1

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

print(count)

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())
  
count = 0

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

print(count)

Submission status: Wrong Answer

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

for i in range(N):
  for j in range(M):
    if LR[j][0] <= i <= LR[j][1]:
      count += 1

print(count)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
a=[[int(i) for i in input().split()] for l in range(m)]
b=0
s=100000000000
for x in a:
    b=max(b,x[0])
    s=min(s,x[1])
print(s-b+1)

Submission status: Wrong Answer

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

L = []
R = []

for i in range(M):
    Li, Ri = map(int, raw_input().split())
    L.append(Li)
    R.append(Ri)

ans = min(R) - max(L) + 1

print ans

Submission status: Wrong Answer

Solution code:
N,M=[int(x) for x in input().rstrip().split()]
l=[]
r=[]
for i in range(M):
    b=input().rstrip().split()
    l.append(int(b[0]))
    r.append(int(b[1]))

L=max(l)
R=min(r)

print(R-L+1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
L_max, R_min = map(int, input().split())
for i in range(m-1):
    l, r = map(int, input().split())
    L_max = max(L_max, l)
    R_min = min(R_min, r)

print(R_min - L_max + 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
L_max = 0
R_min = 10 ** 6
for i in range(m):
    l, r = map(int, input().split())
    L_max = max(L_max, l)
    R_min = min(R_min, r)

print(R_min - L_max + 1)

Submission status: Wrong Answer

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

low = 1
high = N
for i in range(M) :
    l, r = map(int,input().split())
    low = max(low, l)
    high = min(high, r)

print(high - low + 1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
L, R = [0]*M, [0]*M
for i in range(M):
    L[i],R[i] = map(int, input().split())

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

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
s = set(range(N))
for i in range(M):
    x,y = map(int, input().split())
    x -= 1
    y -= 1
    s = s&set(range(x,y))
print(len(s)+1)

Submission status: Wrong Answer

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

a = [0]*m
b = [0]*m
for i in range(m):
  l,r = map(int, input().split())
  a[i] = l
  b[i] = r
  
if b[0] >= a[m-1]:
  print(b[0]-a[m-1]+1)
else:
  print(0)

Submission status: Wrong Answer

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

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

print(b[0]-a[m-1]+1)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
l,r = 1,n
for i in range(m):
    l_tmp,r_tmp = map(int,input().split())
    if l_tmp > l:
        l = l_tmp
    if r_tmp < r:
        r = r_tmp
print(r-l+1)

Submission status: Wrong Answer

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

ans_l = 0
ans_r = 100001

for i in range(m):
    tmp_l,tmp_r = map(int,input().split())
    if ans_l < tmp_l:
        ans_l = tmp_l
    if tmp_r < ans_r:
        ans_r = tmp_r
        
print(ans_r-ans_l+1)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
L,R=10**18,0
for i in range(m):
  l,r=map(int,input().split())
  L=max(L,l)
  R=min(R,r)
if R<L:
  print(0)
else:
  print(R-L+1)

Submission status: Wrong Answer

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

l=1
r=N

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

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

Submission status: Wrong Answer

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

l=0
r=10**5+1

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

print(r-l+1)

Submission status: Wrong Answer

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


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    left = 1
    right = N
    for i in range(M):
        if left < L[i]:
            left = L[i]
        if right > R[i]:
            right = R[i]
    print(right - left + 1)
    return


# Generated by 1.1.6 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:
#abc127 c
n,m=map(int,input().split())
l_max=0
r_mini=n
for i in range(m):
    l,r=map(int,input().split())
    if l>l_max:
        l_max=l
    if r_mini>r:
        r_mini=r
ans=r_mini-l_max+1
print(ans)

Submission status: Wrong Answer

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

begin = 1
end = n
for i in range(m):
    a, b = map(int, input().split())
    if begin > end:
        begin = 0
        end = 0
        break
    if begin <= a:
        if b <= end:
            begin = a
            end = b
        else:
            begin = a
    else:
        if b <= end:
            end = b
        else:
            continue
print(end - begin+1)

Submission status: Wrong Answer

Solution code:
n, m = map(int , input().split())
# L = []
# R = []

l_max = 0
r_min = 10 ** 6
for _ in range(m):
    l, r = map(int, input().split())
    if l_max < l:
        l_max = l
    if r < r_min:
        r_min = r
print(r_min - l_max + 1)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
L = []
R = []
count = 0
for j in range(M):
  l,r = map(int,input().split())
  L.append(l)
  R.append(r)
for i in range(N+1):
  if i > max(L) and i < min(R):
    count += 1
print(count)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
info = []
for _ in range(m):
    l, r = map(int, input().split())
    info.append([l, r])
# print(info)
L = 0
R = 10**5 + 1
for l, r in info:
    Lb, Rb = True, True
    if l < L:
        Lb = False
    elif L <= l <= R:
        L = l
    else:
        print(0)
        exit()
    if R < r:
        Rb = False
    elif L <= r <= R:
        R = r
    else:
        print(0)
        exit()
    if not (Lb or Rb):
        print(0)
        exit()

print(R - L + 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
info = []
for _ in range(m):
    l, r = map(int, input().split())
    info.append([l, r])
print(info)
L = 0
R = 10**5 + 1
for l, r in info:
    Lb, Rb = True, True
    if l < L:
        Lb = False
    elif L <= l <= R:
        L = l
    else:
        print(0)
        exit()
    if R < r:
        Rb = False
    elif L <= r <= R:
        R = r
    else:
        print(0)
        exit()
    if not (Lb or Rb):
        print(0)
        exit()

print(R - L + 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
l = 0
r = float('inf')
for i in range(m):
    li, ri = map(int, input().split())
    lb, rb = True, True
    if l <= li <= r:
        l = li
    elif li < l:
        pass
    else:
        lb = False
    if r >= ri >= l:
        r = ri
    elif ri > r:
        pass
    else:
        rb = False
    if not (lb or rb):
        print(0)
        exit()
    # print(l,r)

print(r - l + 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
l = 0
r = float('inf')
for i in range(m):
    li, ri = map(int, input().split())
    lb, rb = True, True
    if l <= li <= r:
        l = li
    elif li < l:
        continue
    else:
        lb = False
    if r >= ri >= l:
        r = ri
    elif ri > r:
        continue
    else:
        rb = False
    if not (lb or rb):
        print(0)
        exit()
    # print(l,r)

print(r - l + 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
l = 0
r = float('inf')
for i in range(m):
    li, ri = map(int, input().split())
    lb, rb = True, True
    if l <= li and li <= r:
        l = li
    else:
        lb = False
    if r >= ri and ri >= l:
        r = ri
    else:
        rb = False
    if not (lb or rb):
        print(0)
        exit()
    # print(l,r)

print(r - l + 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
l = 0
r = float('inf')
for i in range(m):
    li, ri = map(int, input().split())
    lb, rb = True, True
    if l < li and li <= r:
        l = li
    else:
        lb = False
    if r > ri and ri >= l:
        r = ri
    else:
        rb = False
    if not (lb or rb):
        print(0)
        exit()
    # print(l,r)

print(r - l + 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
l = 0
r = float('inf')
for i in range(m):
    li, ri = map(int, input().split())
    if l < li and li <= r:
        l = li
    if r > ri and ri >= l:
        r = ri

print(r - l + 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
l = 0
r = float('inf')
for i in range(m):
    li, ri = map(int, input().split())
    if l < li:
        l = li
    if r > ri:
        r = ri

print(r - l + 1)

Submission status: Wrong Answer

Solution code:
N, M = [int(i) for i in input().strip().split()]
L = [0] * M
R = [0] * M
for i in range(M):
    L[i], R[i] = [int(i) for i in input().strip().split()]

LL, RR = L[0], R[0]
is_zero = False
for i in range(1, M):
    if RR < L[i]:
        is_zero = True
        break
    elif LL <= L[i] <= RR:
        LL = max(LL, L[i])
        RR = min(RR, R[i])
    elif L[i] < LL and LL <= R[i] <= RR:
        RR = min(RR, R[i])
    else:
        is_zero = True
        break

if is_zero:
    print(0)
else:
    print(RR - LL + 1)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n, m = map(int,input().split())
l_ls = []
r_ls = []

for _ in range(m):
    l,r = map(int,input().split())
    l_ls.append(l)
    r_ls.append(r)

count = 0
small = max(l_ls)
big = min(r_ls)

print(min(big-small+1,0))

Submission status: Wrong Answer

Solution code:
n, m = map(int,input().split())
l_ls = []
r_ls = []

for _ in range(m):
    l,r = map(int,input().split())
    l_ls.append(l)
    r_ls.append(r)

count = 0
small = max(l_ls)
big = min(r_ls)

print(big-small+1)

Submission status: Wrong Answer

Solution code:
n, m = map(int,input().split())
l_ls = []
r_ls = []

for _ in range(m):
    l,r = map(int,input().split())
    l_ls.append(l)
    r_ls.append(r)

count = 0
small = max(l_ls)
big = min(r_ls)
for i in range(n):
    if i >= small and i <= big:
        count += 1
print(count)

Submission status: Wrong Answer

Solution code:
n, m = [int(i) for i in input().split()]
l = []
r = []
for _ in range(m):
    i, k = [int(i) for i in input().split()]
    l.append(i)
    r.append(k)

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

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
MIN,MAX=map(int,input().split())
for _ in range(m-1):
    l,r=map(int,input().split())
    MIN=max(MIN,l)
    MAX=min(MAX,r)
print(MAX-MIN+1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
left, right = 1, n
for _ in range(m):
  l, r = map(int, input().split())
  if l > left:
    left = l
  if r < right:
    right = r
  if l > r:
    print(0)
    quit()
print(right - left + 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
left, right = 1, n
for _ in range(m):
  l, r = map(int, input().split())
  if l > left:
    left = l
  if r < right:
    right = r
  if l >= r:
    print(0)
    quit()
print(right - left + 1)

Submission status: Wrong Answer

Solution code:
n,m = map(int, input().split())
l,r = 0,n
for i in range(m):
    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=[]
R=[]
for i in range (M):
	X,Y=map(int,input().split())
	L.append(X)
	R.append(Y)
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):
  L[i],R[i]=map(int,input().split())
s=0
for j in range(L[0],R[0]+1):
  if all(L[k]<=j<=R[k] for k in range(j,M)):
    s+=1
print(s)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

def main():
  n, m = map(int, input().strip().split())
  ls, rs = [], []
  for _ in range(m):
    l, r = map(int, input().strip().split())
    ls.append(l)
    rs.append(r)
  l = max(ls)
  r = min(rs)
  print(r - l + 1)

if __name__=='__main__':
  main()

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
lr=[list(map(int,input().split())) for _ in range(m)]
print(sorted(lr,key=lambda x:x[1])[0][1]-sorted(lr,key=lambda x:x[0])[-1][0]+1)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
import heapq
L=[]
R=[]
for i in range(M):
  l,r=map(int,input().split())
  heapq.heappush(L, -l)
  heapq.heappush(R, r)
l=heapq.heappop(L)
r=heapq.heappop(R)
print(r+l+1)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
G=[list(map(int,input().split())) for _ in range(M)]
mi=0
ma=float("inf")
for i in range(M):
    if G[i][0]>mi:
        mi=G[i][0]
    if G[i][1]<ma:
        ma=G[i][1]
    print(mi,ma)
print(max(ma-mi+1,0))

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
G=[list(map(int,input().split())) for _ in range(M)]
min=0
max=float("inf")
for i in range(M):
    if G[i][0]>min:
        min=G[i][0]
    if G[i][1]<max:
        max=G[i][1]
print(max-min+1)

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:
import numpy as np

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

LR = np.ones([M,2])

for i in range(M):
    LR[i][0],LR[i][1] = [int(i) for i in input().split()]
    
max_L = max(LR[:,0])
min_R = min(LR[:,1])
if max_L <+ min_R:
    ans = int(min_R - max_L+1)
else:
    ans =0
    
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

LR = np.ones([M,2])

for i in range(M):
    LR[i][0],LR[i][1] = [int(i) for i in input().split()]
    
max_L = max(LR[:,0])
min_R = min(LR[:,1])
if max_L < min_R:
    ans = int(min_R - max_L)
else:
    ans =0
    
print(ans)

Submission status: Wrong Answer

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

f_inf = float("inf")
L_max = 0
R_min = f_inf
for L, R in LR:
    L_max = max(L_max, L)
    R_min = min(R_min, R)

print(R_min - L_max + 1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
l_max = 1
r_min = N + 1
for _ in range(M):
    L, R = map(int, input().split())
    if l_max < L:
        l_max = L
    if r_min > R:
        r_min = R
if r_min < l_max:
    print(r_min - l_max + 1)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
l_max = 1
r_min = N + 1
for _ in range(M):
    L, R = map(int, input().split())
    if l_max <= L:
        l_max = L
    if r_min >= R:
        r_min = R
print(r_min - l_max + 1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
l_max = 0
r_min = N + 1
for _ in range(M):
    L, R = map(int, input().split())
    if l_max < L:
        l_max = L
    if r_min > R:
        r_min = R
print(r_min - l_max + 1)