# import bisect import copy import heapq import itertools import math import operator import random import sys from bisect import bisect, bisect_left, bisect_right, insort from collections import Counter, deque from fractions import Fraction from functools import cmp_to_key, lru_cache, partial from inspect import currentframe from math import ceil, gcd, log10, pi, sqrt from typing import Iterable, Iterator, List, Tuple, TypeVar, Union # import pypyjit # pypyjit.set_param('max_unroll_recursion=-1') # import string # import networkx as nx input = sys.stdin.readline sys.setrecursionlimit(10000000) # mod = 10 ** 9 + 7 mod = 998244353 # mod = 1 << 128 # mod = 10 ** 30 + 1 INF = 1 << 61 DIFF = 10 ** -9 DX = [1, 0, -1, 0, 1, 1, -1, -1] DY = [0, 1, 0, -1, 1, -1, 1, -1] def read_values(): return tuple(map(int, input().split())) def read_index(): return tuple(map(lambda x: int(x) - 1, input().split())) def read_list(): return list(read_values()) def read_lists(N): return [read_list() for _ in range(N)] def dprint(*values): print(*values, file=sys.stderr) def dprint2(*values): names = {id(v): k for k, v in currentframe().f_back.f_locals.items()} dprint(", ".join(f"{names.get(id(value), '???')}={repr(value)}" for value in values)) def f1(N, M, L): G = [set() for _ in range(N)] GR = [set() for _ in range(N)] for i, (u, v) in enumerate(L): G[u].add((v, i)) GR[v].add((u, i)) T = [0] * N F = [False] * M for i in range(M): if F[i]: continue F[i] = True u0, v0 = L[i] G[u0].discard((v0, i)) GR[v0].discard((u0, i)) u = v0 while True: for v, k in G[u]: if F[k]: continue F[k] = True break else: break G[u].discard((v, k)) GR[v].discard((u, k)) u = v u1 = u v = u0 while True: for u, k in GR[v]: if F[k]: continue F[k] = True break else: break GR[v].discard((u, k)) G[u].discard((v, k)) v = u v1 = v T[u1] += 1 T[v1] += 1 return sum(T) // 2 - 1 def f2(N, M, L): G = [set() for _ in range(N)] GR = [set() for _ in range(N)] for i, (u, v) in enumerate(L): G[u].add((v, i)) GR[v].add((u, i)) T = [0] * N F = [False] * M for i in reversed(range(M)): if F[i]: continue F[i] = True u0, v0 = L[i] G[u0].discard((v0, i)) GR[v0].discard((u0, i)) u = v0 while True: for v, k in G[u]: if F[k]: continue F[k] = True break else: break G[u].discard((v, k)) GR[v].discard((u, k)) u = v u1 = u v = u0 while True: for u, k in GR[v]: if F[k]: continue F[k] = True break else: break GR[v].discard((u, k)) G[u].discard((v, k)) v = u v1 = v T[u1] += 1 T[v1] += 1 return sum(T) // 2 - 1 def main(): N, M = read_values() L = [] for _ in range(M): u, v = read_index() L.append((u, v)) print(min(f1(N, M, L), f2(N, M, L))) if __name__ == "__main__": main()