N,M=map(int,input().split()) g=dict() e=[] for i in range(M): b,c=map(int,input().split()) e.append((c,b)) g[c]=[] for a,b in e: g[a].append(b) e.sort(reverse=True) to=dict() ans=N*(N+1)//2 def dfs(cur,v): global ans for dst in g[cur]: if dst in to or dst>v: continue to[dst]=v ans+=v-dst if dst in g: dfs(dst,v) for src,tmp in e: dfs(src,src) print(ans)