import sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] class BitMax: def __init__(self, n): self.inf=10**9 self.n = n + 3 self.table = [-self.inf] * (self.n + 1) def update(self, i, x): i += 1 while i <= self.n: if x > self.table[i]: self.table[i] = x i += i & -i def max(self, i): i += 1 res = -self.inf while i > 0: if self.table[i] > res: res = self.table[i] i -= i & -i return res class BitMin: def __init__(self, n): self.inf=10**9 self.n = n + 3 self.table = [self.inf] * (self.n + 1) def update(self, i, x): i += 1 while i <= self.n: if x < self.table[i]: self.table[i] = x i += i & -i def min(self, i): i += 1 res = self.inf while i > 0: if self.table[i] < res: res = self.table[i] i -= i & -i return res d,q=MI() s=set() ab=[] for _ in range(q): a,b=MI() b+=1 ab.append((a,b)) s.add(a) s.add(b) dec=[a for a in sorted(s)] enc={a:i for i,a in enumerate(dec)} n=len(dec) ll=BitMin(n) rr=BitMax(n) ans=0 for a,b in ab: a=enc[a] b=enc[b] ll.update(n-b,a) rr.update(a,b) l=ll.min(n-a) r=rr.max(b) ll.update(n-r,l) rr.update(l,r) ans=max(ans,dec[r]-dec[l]) print(ans)