import sys input = sys.stdin.readline from operator import itemgetter N,M=map(int,input().split()) E=dict() for i in range(M): x,y=map(int,input().split()) if y in E: E[y].append(x) else: E[y]=[x] USESET=set() SCORE=N*(N+1)//2 KEYS=sorted(E.keys(),reverse=True) for i in KEYS: if i in USESET: continue Q=[i] while Q: x=Q.pop() if not (x in E): continue for to in E[x]: if to in USESET: continue else: USESET.add(to) if i>to: SCORE+=i-to Q.append(to) print(SCORE)