#include using namespace std; void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); } int main() { fast_io(); int n, m; cin >> n >> m; vector deg_i(n), deg_o(n); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; deg_i[v - 1]++; deg_o[u - 1]++; } vector diff(n); for (int i = 0; i < n; i++) { diff[i] = deg_i[i] - deg_o[i]; } sort(diff.begin(), diff.end()); long long ans_full = 0; for (int i = 0; i < n; i++) { ans_full += abs(diff[i]); } long long ans_semi = abs(diff[0] + 1) + abs(diff[n - 1] - 1); for (int i = 1; i < n - 1; i++) { ans_semi += abs(diff[i]); } cout << min(ans_full, ans_semi) / 2 << endl; }