# Reference: https://github.com/shakayami/ACL-for-python class dsu(): n=1 parent_or_size=[-1 for i in range(n)] def __init__(self,N): self.n=N self.parent_or_size=[-1 for i in range(N)] def merge(self,a,b): assert 0<=a0: result2.append(result[i]) return result2 N, M = map(int, input().split()) tree = dsu(N) mem = [[]for i in range(N)] for i in range(M): r, c = map(int, input().split()) mem[r-1].append(c-1) for i in range(N): for j in range(1,len(mem[i])): tree.merge(mem[i][j], mem[i][j-1]) cnt = [0]*N x = [] y = [] for i in range(N): if len(mem[i]) == 0: x.append(1) y.append(0) else: cnt[tree.leader(mem[i][0])] += 1 for i in range(N): if tree.leader(i) == i: x.append(cnt[i]) y.append(tree.size(i)) inf = int(1e18) ans = inf K = len(x) dp = [inf]*(N+1) dp[0] = 0 for i in range(K): for j in reversed(range(N+1)): if j < x[i]: break dp[j] = min(dp[j], dp[j-x[i]]+y[i]) for i in range(N+1): if dp[i] == inf: continue ans = min(ans, N*N+2*i*dp[i]-dp[i]*N-i*N-M) for i in range(K): x[i], y[i] = y[i], x[i] dp = [inf]*(N+1) dp[0] = 0 for i in range(K): for j in reversed(range(N+1)): if j < x[i]: break dp[j] = min(dp[j], dp[j-x[i]]+y[i]) for i in range(N+1): if dp[i] == inf: continue ans = min(ans, N*N+2*i*dp[i]-dp[i]*N-i*N-M) print(ans)