# import pypyjit # pypyjit.set_param("max_unroll_recursion=-1") from collections import * from functools import * from itertools import * from heapq import * import sys, math # sys.setrecursionlimit(3*10**5) input = sys.stdin.readline class SegTree: """ Segment Tree """ def __init__(self, init_val, segfunc, ide_ele): """ 初期化 init_val: 配列の初期値 """ n = len(init_val) self.segfunc = segfunc self.ide_ele = ide_ele self.num = 1 << (n - 1).bit_length() self.tree = [ide_ele] * 2 * self.num # 配列の値を葉にセット for i in range(n): self.tree[self.num + i] = init_val[i] # 構築していく for i in range(self.num - 1, 0, -1): self.tree[i] = segfunc(self.tree[2 * i], self.tree[2 * i + 1]) def update(self, k, x): """ k番目の値をxに更新 k: index(0-index) x: update value """ k += self.num self.tree[k] = x while k > 1: tk = k>>1 self.tree[tk] = self.segfunc(self.tree[tk<<1], self.tree[(tk<<1)+1]) k >>= 1 def query(self, l, r): """ [l, r)のsegfuncしたものを得る l: index(0-index) r: index(0-index) """ res_l = self.ide_ele res_r = self.ide_ele l += self.num r += self.num while l < r: if l & 1: res_l = self.segfunc(res_l, self.tree[l]) l += 1 if r & 1: res_r = self.segfunc(self.tree[r - 1], res_r) l >>= 1 r >>= 1 res = self.segfunc(res_l,res_r) return res class LargestSubarray: ide_ele = (0,0,0,0) def segfunc(self,x,y): z = [0,0,0,0] z[0] = max(x[0],y[0],x[2]+y[1]) z[1] = max(x[1],x[3]+y[1]) z[2] = max(y[2],x[2]+y[3]) z[3] = x[3]+y[3] return tuple(z) def __init__(self,A): self.T = SegTree([(a,a,a,a) for a in A],self.segfunc,self.ide_ele) def update(self,k,x): self.T.update(k,(x,x,x,x)) def query(self,l,r): return self.T.query(l,r)[0] D,Q = map(int,input().split()) AB = [tuple(map(int,input().split())) for _ in range(Q)] X = [] for a,b in AB: X.append(a) X.append(b+1) X = list(set(X)) X.sort() inv = {x:i for i,x in enumerate(X)} N = len(X) I = [X[i+1]-X[i] for i in range(N-1)]+[0] S = SegTree([(0,i) for i in range(N)],min,(1,N)) T = LargestSubarray([-D]*N) for a,b in AB: v,idx = S.query(inv[a],inv[b+1]) while v==0: T.update(idx,I[idx]) S.update(idx,(1,idx)) v,idx = S.query(inv[a],inv[b+1]) print(T.query(0,N))