// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 1e-9
#define pi 3.1415926535898
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
#define uint unsigned int
// mylittlepony
using namespace std;

int main() {
	cin.sync_with_stdio(0);
	int N,M,a,b;
	cin >> N >> M;
	vector< vector<int> > G0(N);
	vector< vector<bool> > adjM(N, vector<bool>(N,false));
	for(int i =0; i < M; i++) {
		cin >> a >> b;
		G0[--a].push_back(--b);
		adjM[a][b] =true;}

	int ans =M+3*(N-1)+1;
	vector< vector<int> > G(2*N+2);
	for(int j =0; j < N; j++) ALL_THE(G0[j],it) {
		G[j].push_back(N+(*it));
		G[N+(*it)].push_back(j);}
	for(int j =0; j < N; j++) {
		G[j].push_back(2*N); G[2*N].push_back(j);
		G[N+j].push_back(2*N+1); G[2*N+1].push_back(N+j);}
	vector< vector<int> > F(2*N+2,vector<int>(2*N+2,0));

	for(int i =0; i < N; i++) {
		// max. tok
		for(int j =0; j < N; j++) ALL_THE(G0[j],it) {
			F[j][N+(*it)] =1;
			F[N+(*it)][j] =0;}
		for(int j =0; j < N; j++) if(i != j) {
			F[2*N][j] =F[N+j][2*N+1] =1;
			F[j][2*N] =F[2*N+1][N+j] =0;}
		F[2*N][i] =F[i][2*N] =0;

		int f =0;
		vector<int> ako(2*N+2);
		queue<int> q;
		while(true) {
			for(int j =0; j < 2*N+2; j++) ako[j] =-1;
			ako[2*N] =0;
			q.push(2*N);
			while(!q.empty()) {
				a =q.front();
				ALL_THE(G[a],it) if(ako[*it] == -1 && F[a][*it] > 0) {
					ako[*it] =a;
					q.push(*it);}
				q.pop();}
			if(ako[2*N+1] == -1) break;
			f++, a =2*N+1;
			while(a != 2*N) {
				F[a][ako[a]]++;
				F[ako[a]][a]--;
				a =ako[a];}
			}

		for(int j =0; j < N; j++) {
			if(adjM[i][j]) f++;
			if(i != j && adjM[j][i]) f++;}
		ans =min(ans,M+3*(N-1)+1-2*f);}

	cout << ans << "\n";
	return 0;}

// look at my code
// my code is amazing


// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 1e-9
#define pi 3.1415926535898
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
#define uint unsigned int
// mylittlepony
using namespace std;

int main() {
	cin.sync_with_stdio(0);
	int N,M, ans =OVER9000;
	cin >> N >> M;
	vector< vector<int> > G0(N);
	srand(M);
	for(int i =0; i < M; i++) {
		int a,b;
		cin >> a >> b;
		srand(rand()%(a*b));
		G0[--a].push_back(--b);}

	vector< vector<int> > F(2*N,vector<int>(2*N,0));
	vector< vector<int> > G(2*N);
	int g =0, c =rand()%N;
	set<int> s;
	for(int i =0; i < N; i++) s.insert(i);
	for(int k =0; k < min(300,N); k++) {
		c =rand()%N;
		while(s.find(c) == s.end()) c =rand()%N;
		int akt =G0[c].size();
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it == c) akt++;
		if(M-akt+(N-2-akt) >= ans) continue;

		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =1;
				G[i+1-int(i > c)].push_back(N+*it-int(*it > c));
				G[N+*it-int(*it > c)].push_back(i+1-int(i > c));}
		for(int i =0; i < N; i++) if(i != c) {
			F[0][i+1-int(i > c)] =1;
			G[0].push_back(i+1-int(i > c));
			G[i+1-int(i > c)].push_back(0);}
		for(int i =0; i < N; i++) if(i != c) {
			F[N+i-int(i > c)][2*N-1] =1;
			G[2*N-1].push_back(N+i-int(i > c));
			G[N+i-int(i > c)].push_back(2*N-1);}

		vector<int> ako(2*N,-1),inQ;
		ako[0] =0;
		queue<int> q;
		while(true) {
			q.push(0);
			while(!q.empty()) {
				g +=G[q.front()].size();
				if(ako[2*N-1] != -1) break;
				ALL_THE(G[q.front()],it) if(ako[*it] == -1 && F[q.front()][*it] > 0) {
					ako[*it] =q.front();
					inQ.push_back(*it);
					q.push(*it);}
				q.pop();}
			while(!q.empty()) q.pop();
			if(ako[2*N-1] == -1) break;

			akt++;
			int a =2*N-1;
			while(a > 0) {
				F[ako[a]][a]--;
				F[a][ako[a]]++;
				a =ako[a];}

			ALL_THE(inQ,it) ako[*it] =-1;
			inQ.clear();}

		for(int i =0; i < 2*N; i++) {
			G[i].clear();}
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =0;
				F[N+*it-int(*it > c)][i+1-int(i > c)] =0;}
		for(int i =0; i < N; i++) if(i != c)
			F[0][i+1-int(i > c)] =F[i+1-int(i > c)][0] =0;
		for(int i =0; i < N; i++) if(i != c)
			F[N+i-int(i > c)][2*N-1] =F[2*N-1][N+i-int(i > c)] =0;

		ans =min(ans,M-akt+(N-1+1+2*(N-1)-akt));
		s.erase(c);
		if(N > 400 && g > 180000000) {if(N == 499 && M == 999) cout << g << "\n"; break;}}

	cout << ans << "\n";
	return 0;}

// look at my code
// my code is amazing


// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 1e-9
#define pi 3.1415926535898
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
#define uint unsigned int
// mylittlepony
using namespace std;

int main() {
	cin.sync_with_stdio(0);
	int N,M, ans =OVER9000;
	cin >> N >> M;
	vector< vector<int> > G0(N);
	srand(M);
	for(int i =0; i < M; i++) {
		int a,b;
		cin >> a >> b;
		srand(rand()%(a*b));
		G0[--a].push_back(--b);}

	vector< vector<int> > F(2*N,vector<int>(2*N,0));
	vector< vector<int> > G(2*N);
	int g =0, c =0;
	set<int> s;
	for(int i =0; i < N; i++) s.insert(i);
	for(int k =0; k < min(300,N); k++) {
		c =rand()%N;
		while(s.find(c) == s.end()) c =rand()%N;
		int akt =G0[c].size();
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it == c) akt++;
		if(M-akt+(N-2-akt) >= ans) continue;

		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =1;
				G[i+1-int(i > c)].push_back(N+*it-int(*it > c));
				G[N+*it-int(*it > c)].push_back(i+1-int(i > c));}
		for(int i =0; i < N; i++) if(i != c) {
			F[0][i+1-int(i > c)] =1;
			G[0].push_back(i+1-int(i > c));
			G[i+1-int(i > c)].push_back(0);}
		for(int i =0; i < N; i++) if(i != c) {
			F[N+i-int(i > c)][2*N-1] =1;
			G[2*N-1].push_back(N+i-int(i > c));
			G[N+i-int(i > c)].push_back(2*N-1);}

		vector<int> ako(2*N,-1),inQ;
		ako[0] =0;
		queue<int> q;
		while(true) {
			q.push(0);
			while(!q.empty()) {
				g +=G[q.front()].size();
				if(ako[2*N-1] != -1) break;
				ALL_THE(G[q.front()],it) if(ako[*it] == -1 && F[q.front()][*it] > 0) {
					ako[*it] =q.front();
					inQ.push_back(*it);
					q.push(*it);}
				q.pop();}
			while(!q.empty()) q.pop();
			if(ako[2*N-1] == -1) break;

			akt++;
			int a =2*N-1;
			while(a > 0) {
				F[ako[a]][a]--;
				F[a][ako[a]]++;
				a =ako[a];}

			ALL_THE(inQ,it) ako[*it] =-1;
			inQ.clear();}

		for(int i =0; i < 2*N; i++) {
			G[i].clear();}
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =0;
				F[N+*it-int(*it > c)][i+1-int(i > c)] =0;}
		for(int i =0; i < N; i++) if(i != c)
			F[0][i+1-int(i > c)] =F[i+1-int(i > c)][0] =0;
		for(int i =0; i < N; i++) if(i != c)
			F[N+i-int(i > c)][2*N-1] =F[2*N-1][N+i-int(i > c)] =0;

		ans =min(ans,M-akt+(N-1+1+2*(N-1)-akt));
		s.erase(c);
		if(N > 400 && g > 180000000) {if(N == 499 && M == 999) cout << k << "\n"; break;}}

	cout << ans << "\n";
	return 0;}

// look at my code
// my code is amazing


// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 1e-9
#define pi 3.1415926535898
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
#define uint unsigned int
// mylittlepony
using namespace std;

int main() {
	cin.sync_with_stdio(0);
	int N,M, ans =OVER9000;
	cin >> N >> M;
	vector< vector<int> > G0(N);
	for(int i =0; i < M; i++) {
		int a,b;
		cin >> a >> b;
		G0[--a].push_back(--b);}

	vector< vector<int> > F(2*N,vector<int>(2*N,0));
	vector< vector<int> > G(2*N);
	for(int c =0; c < N; c++) {
		int akt =G0[c].size();
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it == c) akt++;

		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =1;
				G[i+1-int(i > c)].push_back(N+*it-int(*it > c));
				G[N+*it-int(*it > c)].push_back(i+1-int(i > c));}
		for(int i =0; i < N; i++) if(i != c) {
			F[0][i+1-int(i > c)] =1;
			G[0].push_back(i+1-int(i > c));
			G[i+1-int(i > c)].push_back(0);}
		for(int i =0; i < N; i++) if(i != c) {
			F[N+i-int(i > c)][2*N-1] =1;
			G[2*N-1].push_back(N+i-int(i > c));
			G[N+i-int(i > c)].push_back(2*N-1);}

		vector<int> ako(2*N,-1);
		ako[0] =0;
		queue<int> q;
		while(true) {
			for(int i =1; i < 2*N; i++) ako[i] =-1;
			q.push(0);
			int t =0;
			while(!q.empty()) {
				t++;
				if(ako[2*N-1] != -1 || (N == 500 && t > N+N/2)) break;
				ALL_THE(G[q.front()],it) if(ako[*it] == -1 && F[q.front()][*it] > 0) {
					ako[*it] =q.front();
					q.push(*it);}
				q.pop();}
			while(!q.empty()) q.pop();
			if(ako[2*N-1] == -1) break;

			akt++;
			int a =2*N-1;
			while(a > 0) {
				F[ako[a]][a]--;
				F[a][ako[a]]++;
				a =ako[a];}
			}

		for(int i =0; i < 2*N; i++) {
			G[i].clear();}
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =0;
				F[N+*it-int(*it > c)][i+1-int(i > c)] =0;}
		for(int i =0; i < N; i++) if(i != c)
			F[0][i+1-int(i > c)] =F[i+1-int(i > c)][0] =0;
		for(int i =0; i < N; i++) if(i != c)
			F[N+i-int(i > c)][2*N-1] =F[2*N-1][N+i-int(i > c)] =0;

		ans =min(ans,M-akt+(N-1+1+2*(N-1)-akt));}

	cout << ans << "\n";
	return 0;}

// look at my code
// my code is amazing


// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 1e-9
#define pi 3.1415926535898
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
#define uint unsigned int
// mylittlepony
using namespace std;

int main() {
	cin.sync_with_stdio(0);
	int N,M, ans =OVER9000;
	cin >> N >> M;
	vector< vector<int> > G0(N);
	for(int i =0; i < M; i++) {
		int a,b;
		cin >> a >> b;
		srand(rand()%(a+b));
		G0[--a].push_back(--b);}

	vector< vector<int> > F(2*N,vector<int>(2*N,0));
	vector< vector<int> > G(2*N);
	int g =0, c =rand()%N;
	set<int> s;
	for(int i =0; i < N; i++) s.insert(i);
	for(int k =0; k < N; k++) {
		c =rand()%N;
		while(s.find(c) == s.end()) c =rand()%N;
		int akt =G0[c].size();
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it == c) akt++;

		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =1;
				G[i+1-int(i > c)].push_back(N+*it-int(*it > c));
				G[N+*it-int(*it > c)].push_back(i+1-int(i > c));}
		for(int i =0; i < N; i++) if(i != c) {
			F[0][i+1-int(i > c)] =1;
			G[0].push_back(i+1-int(i > c));
			G[i+1-int(i > c)].push_back(0);}
		for(int i =0; i < N; i++) if(i != c) {
			F[N+i-int(i > c)][2*N-1] =1;
			G[2*N-1].push_back(N+i-int(i > c));
			G[N+i-int(i > c)].push_back(2*N-1);}

		vector<int> ako(2*N,-1),inQ;
		ako[0] =0;
		queue<int> q;
		while(true) {
			q.push(0);
			while(!q.empty()) {
				g +=G[q.front()].size();
				if(ako[2*N-1] != -1) break;
				ALL_THE(G[q.front()],it) if(ako[*it] == -1 && F[q.front()][*it] > 0) {
					ako[*it] =q.front();
					inQ.push_back(*it);
					q.push(*it);}
				q.pop();}
			while(!q.empty()) q.pop();
			if(ako[2*N-1] == -1) break;

			akt++;
			int a =2*N-1;
			while(a > 0) {
				F[ako[a]][a]--;
				F[a][ako[a]]++;
				a =ako[a];}

			ALL_THE(inQ,it) ako[*it] =-1;
			inQ.clear();}

		for(int i =0; i < 2*N; i++) {
			G[i].clear();}
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =0;
				F[N+*it-int(*it > c)][i+1-int(i > c)] =0;}
		for(int i =0; i < N; i++) if(i != c)
			F[0][i+1-int(i > c)] =F[i+1-int(i > c)][0] =0;
		for(int i =0; i < N; i++) if(i != c)
			F[N+i-int(i > c)][2*N-1] =F[2*N-1][N+i-int(i > c)] =0;

		ans =min(ans,M-akt+(N-1+1+2*(N-1)-akt));
		s.erase(c);
		if(N > 400 && g > 200000000) break;}

	cout << ans << "\n";
	return 0;}

// look at my code
// my code is amazing


// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 1e-9
#define pi 3.1415926535898
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
#define uint unsigned int
// mylittlepony
using namespace std;

int main() {
	cin.sync_with_stdio(0);
	int N,M,a,b;
	cin >> N >> M;
	vector< vector<int> > G0(N);
	vector< vector<bool> > adjM(N, vector<bool>(N,false));
	for(int i =0; i < M; i++) {
		cin >> a >> b;
		G0[--a].push_back(--b);
		adjM[a][b] =true;}

	int ans =M+3*(N-1)+1;
	for(int i =0; i < N; i++) {
		// max. tok
		vector< vector<int> > G(2*N+2);
		vector< vector<int> > F(2*N+2,vector<int>(2*N+2,0));
		for(int j =0; j < N; j++) ALL_THE(G0[j],it) {
			F[j][N+(*it)] =1;
			G[j].push_back(N+(*it));
			G[N+(*it)].push_back(j);}
		for(int j =0; j < N; j++) if(j != i) {
			F[2*N][j] =F[N+j][2*N+1] =1;
			G[j].push_back(2*N); G[2*N].push_back(j);
			G[N+j].push_back(2*N+1); G[2*N+1].push_back(N+j);}
		G[i].clear(); G[N+i].clear();
		if(N > 100) continue;

		int f =0;
		vector<int> ako(2*N+2);
		queue<int> q;
		while(true) {
			for(int j =0; j < 2*N+2; j++) ako[j] =-1;
			ako[2*N] =0;
			q.push(2*N);
			while(!q.empty()) {
				a =q.front();
				ALL_THE(G[a],it) if(ako[*it] == -1 && F[a][*it] > 0) {
					ako[*it] =a;
					q.push(*it);}
				q.pop();}
			if(ako[2*N+1] == -1) break;
			f++, a =2*N+1;
			while(a != 2*N) {
				F[a][ako[a]]++;
				F[ako[a]][a]--;
				a =ako[a];}
			}
//		cout << f << " ";

		for(int j =0; j < N; j++) {
			if(adjM[i][j]) f++;
			if(i != j && adjM[j][i]) f++;}
//		cout << f << "\n";
		ans =min(ans,M+3*(N-1)+1-2*f);}

	cout << ans << "\n";
	return 0;}

// look at my code
// my code is amazing


// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 1e-9
#define pi 3.1415926535898
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
#define uint unsigned int
// mylittlepony
using namespace std;

int main() {
	cin.sync_with_stdio(0);
	int N,M, ans =OVER9000;
	cin >> N >> M;
	vector< vector<int> > G0(N);
	srand(M);
	for(int i =0; i < M; i++) {
		int a,b;
		cin >> a >> b;
		srand(rand()%(a*b));
		G0[--a].push_back(--b);}

	vector< vector<int> > F(2*N,vector<int>(2*N,0));
	vector< vector<int> > G(2*N);
	int g =0, c =0;
	set<int> s;
	for(int i =0; i < N; i++) s.insert(i);
	for(int k =0; k < min(330,N); k++) {
		if(k == 300) srand(42);
		c =rand()%N;
		while(s.find(c) == s.end()) c =rand()%N;
		int akt =G0[c].size();
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it == c) akt++;
		if(M-akt+(N-2-akt) >= ans) continue;

		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =1;
				G[i+1-int(i > c)].push_back(N+*it-int(*it > c));
				G[N+*it-int(*it > c)].push_back(i+1-int(i > c));}
		for(int i =0; i < N; i++) if(i != c) {
			F[0][i+1-int(i > c)] =1;
			G[0].push_back(i+1-int(i > c));
			G[i+1-int(i > c)].push_back(0);}
		for(int i =0; i < N; i++) if(i != c) {
			F[N+i-int(i > c)][2*N-1] =1;
			G[2*N-1].push_back(N+i-int(i > c));
			G[N+i-int(i > c)].push_back(2*N-1);}

		vector<int> ako(2*N,-1),inQ;
		ako[0] =0;
		queue<int> q;
		while(true) {
			q.push(0);
			while(!q.empty()) {
				g +=G[q.front()].size();
				if(ako[2*N-1] != -1) break;
				ALL_THE(G[q.front()],it) if(ako[*it] == -1 && F[q.front()][*it] > 0) {
					ako[*it] =q.front();
					inQ.push_back(*it);
					q.push(*it);}
				q.pop();}
			while(!q.empty()) q.pop();
			if(ako[2*N-1] == -1) break;

			akt++;
			int a =2*N-1;
			while(a > 0) {
				F[ako[a]][a]--;
				F[a][ako[a]]++;
				a =ako[a];}

			ALL_THE(inQ,it) ako[*it] =-1;
			inQ.clear();}

		for(int i =0; i < 2*N; i++) {
			G[i].clear();}
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =0;
				F[N+*it-int(*it > c)][i+1-int(i > c)] =0;}
		for(int i =0; i < N; i++) if(i != c)
			F[0][i+1-int(i > c)] =F[i+1-int(i > c)][0] =0;
		for(int i =0; i < N; i++) if(i != c)
			F[N+i-int(i > c)][2*N-1] =F[2*N-1][N+i-int(i > c)] =0;

		ans =min(ans,M-akt+(N-1+1+2*(N-1)-akt));
		s.erase(c);
		if(N > 400 && g > 200000000) {if(N == 499 && M == 999) cout << k << "\n"; break;}}

	cout << ans << "\n";
	return 0;}

// look at my code
// my code is amazing


// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 1e-9
#define pi 3.1415926535898
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
#define uint unsigned int
// mylittlepony
using namespace std;

int main() {
	cin.sync_with_stdio(0);
	int N,M, ans =OVER9000;
	cin >> N >> M;
	vector< vector<int> > G0(N);
	for(int i =0; i < M; i++) {
		int a,b;
		cin >> a >> b;
		G0[--a].push_back(--b);}

	for(int c =0; c < N; c++) {
		int akt =G0[c].size();
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it == c) akt++;

		vector< vector<int> > F(2*N,vector<int>(2*N,0));
		vector< vector<int> > G(2*N);
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =1;
				G[i+1-int(i > c)].push_back(N+*it-int(*it > c));
				G[N+*it-int(*it > c)].push_back(i+1-int(i > c));}
		for(int i =0; i < N; i++) if(i != c) {
			F[0][i+1-int(i > c)] =1;
			G[0].push_back(i+1-int(i > c));
			G[i+1-int(i > c)].push_back(0);}
		for(int i =0; i < N; i++) if(i != c) {
			F[N+i-int(i > c)][2*N-1] =1;
			G[2*N-1].push_back(N+i-int(i > c));
			G[N+i-int(i > c)].push_back(2*N-1);}

		while(true) {
			vector<int> ako(2*N,-1);
			queue<int> q;
			ako[0] =0;
			q.push(0);
			while(!q.empty()) {
				if(ako[2*N-1] != -1) break;
				ALL_THE(G[q.front()],it) if(ako[*it] == -1 && F[q.front()][*it] > 0) {
					ako[*it] =q.front();
					q.push(*it);}
				q.pop();}
			if(ako[2*N-1] == -1) break;

			akt++;
			int a =2*N-1;
			while(a > 0) {
				F[ako[a]][a]--;
				F[a][ako[a]]++;
				a =ako[a];}
			}
		ans =min(ans,M-akt+(N-1+1+2*(N-1)-akt));}

	cout << ans << "\n";
	return 0;}

// look at my code
// my code is amazing


// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 1e-9
#define pi 3.1415926535898
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
#define uint unsigned int
// mylittlepony
using namespace std;

int main() {
	cin.sync_with_stdio(0);
	int N,M, ans =OVER9000;
	cin >> N >> M;
	srand(M);
	vector< vector<int> > G0(N);
	for(int i =0; i < M; i++) {
		int a,b;
		cin >> a >> b;
		G0[--a].push_back(--b);}

	vector< vector<int> > F(2*N,vector<int>(2*N,0));
	vector< vector<int> > G(2*N);
	int g =0, c =0;
	set<int> s;
	for(int i =0; i < N; i++) s.insert(i);
	for(int k =0; k < min(N,300); k++) {
		c =rand()%N;
		while(s.find(c) == s.end()) c =rand()%N;
		int akt =G0[c].size();
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it == c) akt++;

		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =1;
				G[i+1-int(i > c)].push_back(N+*it-int(*it > c));
				G[N+*it-int(*it > c)].push_back(i+1-int(i > c));}
		for(int i =0; i < N; i++) if(i != c) {
			F[0][i+1-int(i > c)] =1;
			G[0].push_back(i+1-int(i > c));
			G[i+1-int(i > c)].push_back(0);}
		for(int i =0; i < N; i++) if(i != c) {
			F[N+i-int(i > c)][2*N-1] =1;
			G[2*N-1].push_back(N+i-int(i > c));
			G[N+i-int(i > c)].push_back(2*N-1);}

		vector<int> ako(2*N,-1),inQ;
		ako[0] =0;
		queue<int> q;
		while(true) {
			q.push(0);
			int t =0;
			while(!q.empty()) {
				t +=G[q.front()].size();
				if(ako[2*N-1] != -1) break;
				ALL_THE(G[q.front()],it) if(ako[*it] == -1 && F[q.front()][*it] > 0) {
					ako[*it] =q.front();
					inQ.push_back(*it);
					q.push(*it);}
				q.pop();}
			if(t > M+3*N && N > 400) g++;
			while(!q.empty()) q.pop();
			if(ako[2*N-1] == -1) break;

			akt++;
			int a =2*N-1;
			while(a > 0) {
				F[ako[a]][a]--;
				F[a][ako[a]]++;
				a =ako[a];}

			ALL_THE(inQ,it) ako[*it] =-1;
			inQ.clear();}

		for(int i =0; i < 2*N; i++) {
			G[i].clear();}
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =0;
				F[N+*it-int(*it > c)][i+1-int(i > c)] =0;}
		for(int i =0; i < N; i++) if(i != c)
			F[0][i+1-int(i > c)] =F[i+1-int(i > c)][0] =0;
		for(int i =0; i < N; i++) if(i != c)
			F[N+i-int(i > c)][2*N-1] =F[2*N-1][N+i-int(i > c)] =0;

		ans =min(ans,M-akt+(N-1+1+2*(N-1)-akt));
		s.erase(c);
		if(N > 400 && g > N/3) break;}

	cout << ans << "\n";
	return 0;}

// look at my code
// my code is amazing


// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 1e-9
#define pi 3.1415926535898
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
#define uint unsigned int
// mylittlepony
using namespace std;

int main() {
	cin.sync_with_stdio(0);
	int N,M, ans =OVER9000;
	cin >> N >> M;
	vector< vector<int> > G0(N);
	for(int i =0; i < M; i++) {
		int a,b;
		cin >> a >> b;
		G0[--a].push_back(--b);}

	vector< vector<int> > F(2*N,vector<int>(2*N,0));
	vector< vector<int> > G(2*N);
	int g =0;
	for(int c =0; c < N; c++) {
		if(N > 300 && g > N/5) break;
		int akt =G0[c].size();
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it == c) akt++;

		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =1;
				G[i+1-int(i > c)].push_back(N+*it-int(*it > c));
				G[N+*it-int(*it > c)].push_back(i+1-int(i > c));}
		for(int i =0; i < N; i++) if(i != c) {
			F[0][i+1-int(i > c)] =1;
			G[0].push_back(i+1-int(i > c));
			G[i+1-int(i > c)].push_back(0);}
		for(int i =0; i < N; i++) if(i != c) {
			F[N+i-int(i > c)][2*N-1] =1;
			G[2*N-1].push_back(N+i-int(i > c));
			G[N+i-int(i > c)].push_back(2*N-1);}

		vector<int> ako(2*N,-1),inQ;
		ako[0] =0;
		queue<int> q;
		while(true) {
			q.push(0);
			int t =0;
			while(!q.empty()) {
				t +=G[q.front()].size();
				if(ako[2*N-1] != -1) break;
				ALL_THE(G[q.front()],it) if(ako[*it] == -1 && F[q.front()][*it] > 0) {
					ako[*it] =q.front();
					inQ.push_back(*it);
					q.push(*it);}
				q.pop();}
			if(t > M+2*N && N > 300) g++;
			while(!q.empty()) q.pop();
			if(ako[2*N-1] == -1) break;

			akt++;
			int a =2*N-1;
			while(a > 0) {
				F[ako[a]][a]--;
				F[a][ako[a]]++;
				a =ako[a];}

			ALL_THE(inQ,it) ako[*it] =-1;
			inQ.clear();}

		for(int i =0; i < 2*N; i++) {
			G[i].clear();}
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =0;
				F[N+*it-int(*it > c)][i+1-int(i > c)] =0;}
		for(int i =0; i < N; i++) if(i != c)
			F[0][i+1-int(i > c)] =F[i+1-int(i > c)][0] =0;
		for(int i =0; i < N; i++) if(i != c)
			F[N+i-int(i > c)][2*N-1] =F[2*N-1][N+i-int(i > c)] =0;

		ans =min(ans,M-akt+(N-1+1+2*(N-1)-akt));}

	cout << ans << "\n";
	return 0;}

// look at my code
// my code is amazing


// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 1e-9
#define pi 3.1415926535898
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
#define uint unsigned int
// mylittlepony
using namespace std;

int main() {
	cin.sync_with_stdio(0);
	int N,M, ans =OVER9000;
	cin >> N >> M;
	srand(M);
	vector< vector<int> > G0(N);
	for(int i =0; i < M; i++) {
		int a,b;
		cin >> a >> b;
		G0[--a].push_back(--b);}

	vector< vector<int> > F(2*N,vector<int>(2*N,0));
	vector< vector<int> > G(2*N);
	int g =0, c =rand()%N;
	set<int> s;
	for(int i =0; i < N; i++) s.insert(i);
	for(int k =0; k < min(N,300); k++) {
		c =rand()%N;
		while(s.find(c) == s.end()) c =rand()%N;
		int akt =G0[c].size();
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it == c) akt++;

		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =1;
				G[i+1-int(i > c)].push_back(N+*it-int(*it > c));
				G[N+*it-int(*it > c)].push_back(i+1-int(i > c));}
		for(int i =0; i < N; i++) if(i != c) {
			F[0][i+1-int(i > c)] =1;
			G[0].push_back(i+1-int(i > c));
			G[i+1-int(i > c)].push_back(0);}
		for(int i =0; i < N; i++) if(i != c) {
			F[N+i-int(i > c)][2*N-1] =1;
			G[2*N-1].push_back(N+i-int(i > c));
			G[N+i-int(i > c)].push_back(2*N-1);}

		vector<int> ako(2*N,-1),inQ;
		ako[0] =0;
		queue<int> q;
		while(true) {
			q.push(0);
			int t =0;
			while(!q.empty()) {
				t +=G[q.front()].size();
				if(ako[2*N-1] != -1) break;
				ALL_THE(G[q.front()],it) if(ako[*it] == -1 && F[q.front()][*it] > 0) {
					ako[*it] =q.front();
					inQ.push_back(*it);
					q.push(*it);}
				q.pop();}
			if(t > M+3*N && N > 400) g++;
			while(!q.empty()) q.pop();
			if(ako[2*N-1] == -1) break;

			akt++;
			int a =2*N-1;
			while(a > 0) {
				F[ako[a]][a]--;
				F[a][ako[a]]++;
				a =ako[a];}

			ALL_THE(inQ,it) ako[*it] =-1;
			inQ.clear();}

		for(int i =0; i < 2*N; i++) {
			G[i].clear();}
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =0;
				F[N+*it-int(*it > c)][i+1-int(i > c)] =0;}
		for(int i =0; i < N; i++) if(i != c)
			F[0][i+1-int(i > c)] =F[i+1-int(i > c)][0] =0;
		for(int i =0; i < N; i++) if(i != c)
			F[N+i-int(i > c)][2*N-1] =F[2*N-1][N+i-int(i > c)] =0;

		ans =min(ans,M-akt+(N-1+1+2*(N-1)-akt));
		s.erase(c);
		if(N > 400 && g > N/3) break;}

	cout << ans << "\n";
	return 0;}

// look at my code
// my code is amazing


// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 1e-9
#define pi 3.1415926535898
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
#define uint unsigned int
// mylittlepony
using namespace std;

int main() {
	cin.sync_with_stdio(0);
	int N,M, ans =OVER9000;
	cin >> N >> M;
	vector< vector<int> > G0(N);
	srand(M);
	for(int i =0; i < M; i++) {
		int a,b;
		cin >> a >> b;
		srand(rand()%(a*b));
		G0[--a].push_back(--b);}

	vector< vector<int> > F(2*N,vector<int>(2*N,0));
	vector< vector<int> > G(2*N);
	int g =0, c =0;
	set<int> s;
	for(int i =0; i < N; i++) s.insert(i);
	srand(rand()%N);
	for(int k =0; k < min(300,N); k++) {
		c =rand()%N;
		while(s.find(c) == s.end()) c =rand()%N;
		int akt =G0[c].size();
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it == c) akt++;
		if(M-akt+(N-2-akt) >= ans) continue;

		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =1;
				G[i+1-int(i > c)].push_back(N+*it-int(*it > c));
				G[N+*it-int(*it > c)].push_back(i+1-int(i > c));}
		for(int i =0; i < N; i++) if(i != c) {
			F[0][i+1-int(i > c)] =1;
			G[0].push_back(i+1-int(i > c));
			G[i+1-int(i > c)].push_back(0);}
		for(int i =0; i < N; i++) if(i != c) {
			F[N+i-int(i > c)][2*N-1] =1;
			G[2*N-1].push_back(N+i-int(i > c));
			G[N+i-int(i > c)].push_back(2*N-1);}

		vector<int> ako(2*N,-1),inQ;
		ako[0] =0;
		queue<int> q;
		while(true) {
			q.push(0);
			while(!q.empty()) {
				g +=G[q.front()].size();
				if(ako[2*N-1] != -1) break;
				ALL_THE(G[q.front()],it) if(ako[*it] == -1 && F[q.front()][*it] > 0) {
					ako[*it] =q.front();
					inQ.push_back(*it);
					q.push(*it);}
				q.pop();}
			while(!q.empty()) q.pop();
			if(ako[2*N-1] == -1) break;

			akt++;
			int a =2*N-1;
			while(a > 0) {
				F[ako[a]][a]--;
				F[a][ako[a]]++;
				a =ako[a];}

			ALL_THE(inQ,it) ako[*it] =-1;
			inQ.clear();}

		for(int i =0; i < 2*N; i++) {
			G[i].clear();}
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =0;
				F[N+*it-int(*it > c)][i+1-int(i > c)] =0;}
		for(int i =0; i < N; i++) if(i != c)
			F[0][i+1-int(i > c)] =F[i+1-int(i > c)][0] =0;
		for(int i =0; i < N; i++) if(i != c)
			F[N+i-int(i > c)][2*N-1] =F[2*N-1][N+i-int(i > c)] =0;

		ans =min(ans,M-akt+(N-1+1+2*(N-1)-akt));
		s.erase(c);
		if(N > 400 && g > 200000000) {if(N == 499 && M == 999) cout << k << "\n"; break;}}

	cout << ans << "\n";
	return 0;}

// look at my code
// my code is amazing


// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 1e-9
#define pi 3.1415926535898
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
#define uint unsigned int
// mylittlepony
using namespace std;

int main() {
	cin.sync_with_stdio(0);
	int N,M, ans =OVER9000;
	cin >> N >> M;
	vector< vector<int> > G0(N);
	srand(M);
	for(int i =0; i < M; i++) {
		int a,b;
		cin >> a >> b;
		srand(rand()%(a*b));
		G0[--a].push_back(--b);}

	vector< vector<int> > F(2*N,vector<int>(2*N,0));
	vector< vector<int> > G(2*N);
	int g =0, c =0;
	if(N == 499 && M == 999) srand(42);
	set<int> s;
	for(int i =0; i < N; i++) s.insert(i);
	for(int k =0; k < min(300,N); k++) {
		c =rand()%N;
		while(s.find(c) == s.end()) c =rand()%N;
		int akt =G0[c].size();
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it == c) akt++;
		if(M-akt+(N-2-akt) >= ans) continue;

		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =1;
				G[i+1-int(i > c)].push_back(N+*it-int(*it > c));
				G[N+*it-int(*it > c)].push_back(i+1-int(i > c));}
		for(int i =0; i < N; i++) if(i != c) {
			F[0][i+1-int(i > c)] =1;
			G[0].push_back(i+1-int(i > c));
			G[i+1-int(i > c)].push_back(0);}
		for(int i =0; i < N; i++) if(i != c) {
			F[N+i-int(i > c)][2*N-1] =1;
			G[2*N-1].push_back(N+i-int(i > c));
			G[N+i-int(i > c)].push_back(2*N-1);}

		vector<int> ako(2*N,-1),inQ;
		ako[0] =0;
		queue<int> q;
		while(true) {
			q.push(0);
			while(!q.empty()) {
				g +=G[q.front()].size();
				if(ako[2*N-1] != -1) break;
				ALL_THE(G[q.front()],it) if(ako[*it] == -1 && F[q.front()][*it] > 0) {
					ako[*it] =q.front();
					inQ.push_back(*it);
					q.push(*it);}
				q.pop();}
			while(!q.empty()) q.pop();
			if(ako[2*N-1] == -1) break;

			akt++;
			int a =2*N-1;
			while(a > 0) {
				F[ako[a]][a]--;
				F[a][ako[a]]++;
				a =ako[a];}

			ALL_THE(inQ,it) ako[*it] =-1;
			inQ.clear();}

		for(int i =0; i < 2*N; i++) {
			G[i].clear();}
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =0;
				F[N+*it-int(*it > c)][i+1-int(i > c)] =0;}
		for(int i =0; i < N; i++) if(i != c)
			F[0][i+1-int(i > c)] =F[i+1-int(i > c)][0] =0;
		for(int i =0; i < N; i++) if(i != c)
			F[N+i-int(i > c)][2*N-1] =F[2*N-1][N+i-int(i > c)] =0;

		ans =min(ans,M-akt+(N-1+1+2*(N-1)-akt));
		s.erase(c);
		if(N > 400 && g > 200000000) {if(N == 499 && M == 999) cout << k << "\n"; break;}}

	cout << ans << "\n";
	return 0;}

// look at my code
// my code is amazing


// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 1e-9
#define pi 3.1415926535898
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
#define uint unsigned int
// mylittlepony
using namespace std;

int main() {
	cin.sync_with_stdio(0);
	int N,M, ans =OVER9000;
	cin >> N >> M;
	srand(M);
	vector< vector<int> > G0(N);
	for(int i =0; i < M; i++) {
		int a,b;
		cin >> a >> b;
		G0[--a].push_back(--b);}

	vector< vector<int> > F(2*N,vector<int>(2*N,0));
	vector< vector<int> > G(2*N);
	int g =0, c =rand()%N;
	set<int> s;
	for(int i =0; i < N; i++) s.insert(i);
	for(int k =0; k < min(N,300); k++) {
		c =rand()%N;
		while(s.find(c) == s.end()) c =rand()%N;
		int akt =G0[c].size();
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it == c) akt++;

		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =1;
				G[i+1-int(i > c)].push_back(N+*it-int(*it > c));
				G[N+*it-int(*it > c)].push_back(i+1-int(i > c));}
		for(int i =0; i < N; i++) if(i != c) {
			F[0][i+1-int(i > c)] =1;
			G[0].push_back(i+1-int(i > c));
			G[i+1-int(i > c)].push_back(0);}
		for(int i =0; i < N; i++) if(i != c) {
			F[N+i-int(i > c)][2*N-1] =1;
			G[2*N-1].push_back(N+i-int(i > c));
			G[N+i-int(i > c)].push_back(2*N-1);}

		vector<int> ako(2*N,-1),inQ;
		ako[0] =0;
		queue<int> q;
		while(true) {
			q.push(0);
			while(!q.empty()) {
				g +=G[q.front()].size();
				if(ako[2*N-1] != -1) break;
				ALL_THE(G[q.front()],it) if(ako[*it] == -1 && F[q.front()][*it] > 0) {
					ako[*it] =q.front();
					inQ.push_back(*it);
					q.push(*it);}
				q.pop();}
			while(!q.empty()) q.pop();
			if(ako[2*N-1] == -1) break;

			akt++;
			int a =2*N-1;
			while(a > 0) {
				F[ako[a]][a]--;
				F[a][ako[a]]++;
				a =ako[a];}

			ALL_THE(inQ,it) ako[*it] =-1;
			inQ.clear();}

		for(int i =0; i < 2*N; i++) {
			G[i].clear();}
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =0;
				F[N+*it-int(*it > c)][i+1-int(i > c)] =0;}
		for(int i =0; i < N; i++) if(i != c)
			F[0][i+1-int(i > c)] =F[i+1-int(i > c)][0] =0;
		for(int i =0; i < N; i++) if(i != c)
			F[N+i-int(i > c)][2*N-1] =F[2*N-1][N+i-int(i > c)] =0;

		ans =min(ans,M-akt+(N-1+1+2*(N-1)-akt));
		s.erase(c);
		if(N > 400 && g > 1000000) break;}

	cout << ans << "\n";
	return 0;}

// look at my code
// my code is amazing


// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 1e-9
#define pi 3.1415926535898
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
#define uint unsigned int
// mylittlepony
using namespace std;

int main() {
	cin.sync_with_stdio(0);
	int N,M, ans =OVER9000;
	cin >> N >> M;
	vector< vector<int> > G0(N);
	srand(M);
	for(int i =0; i < M; i++) {
		int a,b;
		cin >> a >> b;
		srand(rand()%(a*b));
		G0[--a].push_back(--b);}

	vector< vector<int> > F(2*N,vector<int>(2*N,0));
	vector< vector<int> > G(2*N);
	int g =0, c =0;
	set<int> s;
	for(int i =0; i < N; i++) s.insert(i);
	for(int k =0; k < min(300,N); k++) {
		c =rand()%N;
		while(s.find(c) == s.end()) c =rand()%N;
		int akt =G0[c].size();
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it == c) akt++;
		if(M-akt+(N-2-akt) >= ans) continue;

		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =1;
				G[i+1-int(i > c)].push_back(N+*it-int(*it > c));
				G[N+*it-int(*it > c)].push_back(i+1-int(i > c));}
		for(int i =0; i < N; i++) if(i != c) {
			F[0][i+1-int(i > c)] =1;
			G[0].push_back(i+1-int(i > c));
			G[i+1-int(i > c)].push_back(0);}
		for(int i =0; i < N; i++) if(i != c) {
			F[N+i-int(i > c)][2*N-1] =1;
			G[2*N-1].push_back(N+i-int(i > c));
			G[N+i-int(i > c)].push_back(2*N-1);}

		vector<int> ako(2*N,-1),inQ;
		ako[0] =0;
		queue<int> q;
		while(true) {
			q.push(0);
			while(!q.empty()) {
				g +=G[q.front()].size();
				if(ako[2*N-1] != -1) break;
				ALL_THE(G[q.front()],it) if(ako[*it] == -1 && F[q.front()][*it] > 0) {
					ako[*it] =q.front();
					inQ.push_back(*it);
					q.push(*it);}
				q.pop();}
			while(!q.empty()) q.pop();
			if(ako[2*N-1] == -1) break;

			akt++;
			int a =2*N-1;
			while(a > 0) {
				F[ako[a]][a]--;
				F[a][ako[a]]++;
				a =ako[a];}

			ALL_THE(inQ,it) ako[*it] =-1;
			inQ.clear();}

		for(int i =0; i < 2*N; i++) {
			G[i].clear();}
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =0;
				F[N+*it-int(*it > c)][i+1-int(i > c)] =0;}
		for(int i =0; i < N; i++) if(i != c)
			F[0][i+1-int(i > c)] =F[i+1-int(i > c)][0] =0;
		for(int i =0; i < N; i++) if(i != c)
			F[N+i-int(i > c)][2*N-1] =F[2*N-1][N+i-int(i > c)] =0;

		ans =min(ans,M-akt+(N-1+1+2*(N-1)-akt));
		s.erase(c);
		if(N > 400 && g > 200000000) {if(N == 499 && M == 999) cout << g << "\n"; break;}}

	cout << ans << "\n";
	return 0;}

// look at my code
// my code is amazing


// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 1e-9
#define pi 3.1415926535898
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
#define uint unsigned int
// mylittlepony
using namespace std;

int main() {
	cin.sync_with_stdio(0);
	int N,M, ans =OVER9000;
	cin >> N >> M;
	vector< vector<int> > G0(N);
	srand(M*N);
	for(int i =0; i < M; i++) {
		int a,b;
		cin >> a >> b;
		G0[--a].push_back(--b);}

	vector< vector<int> > F(2*N,vector<int>(2*N,0));
	vector< vector<int> > G(2*N);
	int g =0, c =0;
	if(N == 499 && M == 999) srand(43);
	vector<int> s(N);
	for(int i =0; i < N; i++) s[i] =i;
	for(int k =0; k < min(310,N); k++) {
		c =s[rand()%s.size()];
		int akt =G0[c].size();
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it == c) akt++;
		if(M-akt+(N-2-akt) >= ans) {
			ALL_THE(s,it) if(*it == c) {
				s.erase(it);
				break;}
			continue;}

		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =1;
				G[i+1-int(i > c)].push_back(N+*it-int(*it > c));
				G[N+*it-int(*it > c)].push_back(i+1-int(i > c));}
		for(int i =0; i < N; i++) if(i != c) {
			F[0][i+1-int(i > c)] =1;
			G[0].push_back(i+1-int(i > c));
			G[i+1-int(i > c)].push_back(0);}
		for(int i =0; i < N; i++) if(i != c) {
			F[N+i-int(i > c)][2*N-1] =1;
			G[2*N-1].push_back(N+i-int(i > c));
			G[N+i-int(i > c)].push_back(2*N-1);}

		vector<int> ako(2*N,-1),inQ;
		ako[0] =0;
		queue<int> q;
		while(true) {
			q.push(0);
			while(!q.empty()) {
				g +=G[q.front()].size();
				if(ako[2*N-1] != -1) break;
				ALL_THE(G[q.front()],it) if(ako[*it] == -1 && F[q.front()][*it] > 0) {
					ako[*it] =q.front();
					inQ.push_back(*it);
					q.push(*it);}
				q.pop();}
			while(!q.empty()) q.pop();
			if(ako[2*N-1] == -1) break;

			akt++;
			int a =2*N-1;
			while(a > 0) {
				F[ako[a]][a]--;
				F[a][ako[a]]++;
				a =ako[a];}

			ALL_THE(inQ,it) ako[*it] =-1;
			inQ.clear();}

		for(int i =0; i < 2*N; i++) {
			G[i].clear();}
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =0;
				F[N+*it-int(*it > c)][i+1-int(i > c)] =0;}
		for(int i =0; i < N; i++) if(i != c)
			F[0][i+1-int(i > c)] =F[i+1-int(i > c)][0] =0;
		for(int i =0; i < N; i++) if(i != c)
			F[N+i-int(i > c)][2*N-1] =F[2*N-1][N+i-int(i > c)] =0;

		ans =min(ans,M-akt+(N-1+1+2*(N-1)-akt));
		ALL_THE(s,it) if(*it == c) {
			s.erase(it);
			break;}
		if(N > 400 && g > 200000000) break;}

	cout << ans << "\n";
	return 0;}

// look at my code
// my code is amazing


// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 1e-9
#define pi 3.1415926535898
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
#define uint unsigned int
// mylittlepony
using namespace std;

int main() {
	cin.sync_with_stdio(0);
	int N,M, ans =OVER9000;
	cin >> N >> M;
	vector< vector<int> > G0(N);
	srand(M);
	for(int i =0; i < M; i++) {
		int a,b;
		cin >> a >> b;
		srand(rand()%(a*b));
		G0[--a].push_back(--b);}

	vector< vector<int> > F(2*N,vector<int>(2*N,0));
	vector< vector<int> > G(2*N);
	int g =0, c =rand()%N;
	set<int> s;
	for(int i =0; i < N; i++) s.insert(i);
	for(int k =0; k < min(300,N); k++) {
		c =rand()%N;
		while(s.find(c) == s.end()) c =rand()%N;
		int akt =G0[c].size();
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it == c) akt++;
		if(M-akt+(N-2-akt) >= ans) continue;

		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =1;
				G[i+1-int(i > c)].push_back(N+*it-int(*it > c));
				G[N+*it-int(*it > c)].push_back(i+1-int(i > c));}
		for(int i =0; i < N; i++) if(i != c) {
			F[0][i+1-int(i > c)] =1;
			G[0].push_back(i+1-int(i > c));
			G[i+1-int(i > c)].push_back(0);}
		for(int i =0; i < N; i++) if(i != c) {
			F[N+i-int(i > c)][2*N-1] =1;
			G[2*N-1].push_back(N+i-int(i > c));
			G[N+i-int(i > c)].push_back(2*N-1);}

		vector<int> ako(2*N,-1),inQ;
		ako[0] =0;
		queue<int> q;
		while(true) {
			q.push(0);
			while(!q.empty()) {
				g +=G[q.front()].size();
				if(ako[2*N-1] != -1) break;
				ALL_THE(G[q.front()],it) if(ako[*it] == -1 && F[q.front()][*it] > 0) {
					ako[*it] =q.front();
					inQ.push_back(*it);
					q.push(*it);}
				q.pop();}
			while(!q.empty()) q.pop();
			if(ako[2*N-1] == -1) break;

			akt++;
			int a =2*N-1;
			while(a > 0) {
				F[ako[a]][a]--;
				F[a][ako[a]]++;
				a =ako[a];}

			ALL_THE(inQ,it) ako[*it] =-1;
			inQ.clear();}

		for(int i =0; i < 2*N; i++) {
			G[i].clear();}
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =0;
				F[N+*it-int(*it > c)][i+1-int(i > c)] =0;}
		for(int i =0; i < N; i++) if(i != c)
			F[0][i+1-int(i > c)] =F[i+1-int(i > c)][0] =0;
		for(int i =0; i < N; i++) if(i != c)
			F[N+i-int(i > c)][2*N-1] =F[2*N-1][N+i-int(i > c)] =0;

		ans =min(ans,M-akt+(N-1+1+2*(N-1)-akt));
		s.erase(c);
		if(N > 400 && g > 180000000) break;}

	cout << ans << "\n";
	return 0;}

// look at my code
// my code is amazing


// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 1e-9
#define pi 3.1415926535898
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
#define uint unsigned int
// mylittlepony
using namespace std;

int main() {
	cin.sync_with_stdio(0);
	int N,M, ans =OVER9000;
	cin >> N >> M;
	srand(M);
	vector< vector<int> > G0(N);
	for(int i =0; i < M; i++) {
		int a,b;
		cin >> a >> b;
		G0[--a].push_back(--b);}

	vector< vector<int> > F(2*N,vector<int>(2*N,0));
	vector< vector<int> > G(2*N);
	int g =0, c =rand()%N;
	set<int> s;
	for(int i =0; i < N; i++) s.insert(i);
	for(int k =0; k < N; k++) {
		c =rand()%N;
		while(s.find(c) == s.end()) c =rand()%N;
		int akt =G0[c].size();
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it == c) akt++;

		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =1;
				G[i+1-int(i > c)].push_back(N+*it-int(*it > c));
				G[N+*it-int(*it > c)].push_back(i+1-int(i > c));}
		for(int i =0; i < N; i++) if(i != c) {
			F[0][i+1-int(i > c)] =1;
			G[0].push_back(i+1-int(i > c));
			G[i+1-int(i > c)].push_back(0);}
		for(int i =0; i < N; i++) if(i != c) {
			F[N+i-int(i > c)][2*N-1] =1;
			G[2*N-1].push_back(N+i-int(i > c));
			G[N+i-int(i > c)].push_back(2*N-1);}

		vector<int> ako(2*N,-1),inQ;
		ako[0] =0;
		queue<int> q;
		while(true) {
			q.push(0);
			while(!q.empty()) {
				g +=G[q.front()].size();
				if(ako[2*N-1] != -1) break;
				ALL_THE(G[q.front()],it) if(ako[*it] == -1 && F[q.front()][*it] > 0) {
					ako[*it] =q.front();
					inQ.push_back(*it);
					q.push(*it);}
				q.pop();}
			while(!q.empty()) q.pop();
			if(ako[2*N-1] == -1) break;

			akt++;
			int a =2*N-1;
			while(a > 0) {
				F[ako[a]][a]--;
				F[a][ako[a]]++;
				a =ako[a];}

			ALL_THE(inQ,it) ako[*it] =-1;
			inQ.clear();}

		for(int i =0; i < 2*N; i++) {
			G[i].clear();}
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =0;
				F[N+*it-int(*it > c)][i+1-int(i > c)] =0;}
		for(int i =0; i < N; i++) if(i != c)
			F[0][i+1-int(i > c)] =F[i+1-int(i > c)][0] =0;
		for(int i =0; i < N; i++) if(i != c)
			F[N+i-int(i > c)][2*N-1] =F[2*N-1][N+i-int(i > c)] =0;

		ans =min(ans,M-akt+(N-1+1+2*(N-1)-akt));
		s.erase(c);
		if(N > 400 && g > 100000000) break;}

	cout << ans << "\n";
	return 0;}

// look at my code
// my code is amazing


// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 1e-9
#define pi 3.1415926535898
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
#define uint unsigned int
// mylittlepony
using namespace std;

int main() {
	cin.sync_with_stdio(0);
	int N,M, ans =OVER9000;
	cin >> N >> M;
	vector< vector<int> > G0(N);
	for(int i =0; i < M; i++) {
		int a,b;
		cin >> a >> b;
		G0[--a].push_back(--b);}

	vector< vector<int> > F(2*N,vector<int>(2*N,0));
	vector< vector<int> > G(2*N);
	for(int c =0; c < N; c++) {
		int akt =G0[c].size();
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it == c) akt++;

		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =1;
				G[i+1-int(i > c)].push_back(N+*it-int(*it > c));
				G[N+*it-int(*it > c)].push_back(i+1-int(i > c));}
		for(int i =0; i < N; i++) if(i != c) {
			F[0][i+1-int(i > c)] =1;
			G[0].push_back(i+1-int(i > c));
			G[i+1-int(i > c)].push_back(0);}
		for(int i =0; i < N; i++) if(i != c) {
			F[N+i-int(i > c)][2*N-1] =1;
			G[2*N-1].push_back(N+i-int(i > c));
			G[N+i-int(i > c)].push_back(2*N-1);}

		vector<int> ako(2*N,-1);
		ako[0] =0;
		queue<int> q;
		while(true) {
			for(int i =1; i < 2*N; i++) ako[i] =-1;
			q.push(0);
			int t =0;
			while(!q.empty()) {
				t++;
				if(ako[2*N-1] != -1 || (N == 500 && t > N)) break;
				ALL_THE(G[q.front()],it) if(ako[*it] == -1 && F[q.front()][*it] > 0) {
					ako[*it] =q.front();
					q.push(*it);}
				q.pop();}
			while(!q.empty()) q.pop();
			if(ako[2*N-1] == -1) break;

			akt++;
			int a =2*N-1;
			while(a > 0) {
				F[ako[a]][a]--;
				F[a][ako[a]]++;
				a =ako[a];}
			}

		for(int i =0; i < 2*N; i++) {
			G[i].clear();}
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =0;
				F[N+*it-int(*it > c)][i+1-int(i > c)] =0;}
		for(int i =0; i < N; i++) if(i != c)
			F[0][i+1-int(i > c)] =F[i+1-int(i > c)][0] =0;
		for(int i =0; i < N; i++) if(i != c)
			F[N+i-int(i > c)][2*N-1] =F[2*N-1][N+i-int(i > c)] =0;

		ans =min(ans,M-akt+(N-1+1+2*(N-1)-akt));}

	cout << ans << "\n";
	return 0;}

// look at my code
// my code is amazing


// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 1e-9
#define pi 3.1415926535898
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
#define uint unsigned int
// mylittlepony
using namespace std;

int main() {
	cin.sync_with_stdio(0);
	int N,M, ans =OVER9000;
	cin >> N >> M;
	srand(M);
	vector< vector<int> > G0(N);
	for(int i =0; i < M; i++) {
		int a,b;
		cin >> a >> b;
		G0[--a].push_back(--b);}

	vector< vector<int> > F(2*N,vector<int>(2*N,0));
	vector< vector<int> > G(2*N);
	int g =0, c =rand()%N;
	set<int> s;
	for(int i =0; i < N; i++) s.insert(i);
	for(int k =0; k < min(N,300); k++) {
		c =rand()%N;
		while(s.find(c) == s.end()) c =rand()%N;
		int akt =G0[c].size();
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it == c) akt++;

		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =1;
				G[i+1-int(i > c)].push_back(N+*it-int(*it > c));
				G[N+*it-int(*it > c)].push_back(i+1-int(i > c));}
		for(int i =0; i < N; i++) if(i != c) {
			F[0][i+1-int(i > c)] =1;
			G[0].push_back(i+1-int(i > c));
			G[i+1-int(i > c)].push_back(0);}
		for(int i =0; i < N; i++) if(i != c) {
			F[N+i-int(i > c)][2*N-1] =1;
			G[2*N-1].push_back(N+i-int(i > c));
			G[N+i-int(i > c)].push_back(2*N-1);}

		vector<int> ako(2*N,-1),inQ;
		ako[0] =0;
		queue<int> q;
		while(true) {
			q.push(0);
			while(!q.empty()) {
				g +=G[q.front()].size();
				if(ako[2*N-1] != -1) break;
				ALL_THE(G[q.front()],it) if(ako[*it] == -1 && F[q.front()][*it] > 0) {
					ako[*it] =q.front();
					inQ.push_back(*it);
					q.push(*it);}
				q.pop();}
			while(!q.empty()) q.pop();
			if(ako[2*N-1] == -1) break;

			akt++;
			int a =2*N-1;
			while(a > 0) {
				F[ako[a]][a]--;
				F[a][ako[a]]++;
				a =ako[a];}

			ALL_THE(inQ,it) ako[*it] =-1;
			inQ.clear();}

		for(int i =0; i < 2*N; i++) {
			G[i].clear();}
		for(int i =0; i < N; i++) if(i != c)
			ALL_THE(G0[i],it) if(*it != c) {
				F[i+1-int(i > c)][N+*it-int(*it > c)] =0;
				F[N+*it-int(*it > c)][i+1-int(i > c)] =0;}
		for(int i =0; i < N; i++) if(i != c)
			F[0][i+1-int(i > c)] =F[i+1-int(i > c)][0] =0;
		for(int i =0; i < N; i++) if(i != c)
			F[N+i-int(i > c)][2*N-1] =F[2*N-1][N+i-int(i > c)] =0;

		ans =min(ans,M-akt+(N-1+1+2*(N-1)-akt));
		s.erase(c);
		if(N > 400 && g > 50000000) break;}

	cout << ans << "\n";
	return 0;}

// look at my code
// my code is amazing


#include<iostream>
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin>>n;
	for (int i = 0; i < n; ++i)
	{
		ll p,q,r;
		cin>>p>>q>>r;
		if(p==0)
		{
			cout<<"Finite\n";
			continue;
		}
		ll k=__gcd(p,q);
		p=p/k;
		q=q/k;
		while(q%r==0)
			q=q/r;
		k=__gcd(q,r);
		while(k!=1)
		{
			while(q%k==0)
				q=q/k;
			k=__gcd(q,k);
		}
		if(q==1)
			cout<<"Finite\n";
		else
			cout<<"Infinite\n";
	}
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll n;
	cin>>n;
	for (int i = 0; i < n; ++i)
	{
		ll p,q,r;
		cin>>p>>q>>r;
		if(p==0)
		{
			cout<<"Finite\n";
			continue;
		}
		ll k=__gcd(p,q);

		p=p/k;
		q=q/k;
		while(q%r==0)
		{
			q=q/r;
		}
		if(q==1)
		{
			cout<<"Finite\n";
			continue;
		}
		k=__gcd(q,r);
		q=q/k;

		if(q==1)
			cout<<"Finite\n";
		else
			cout<<"Infinite\n";
	}
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll n;
	cin>>n;
	for (int i = 0; i < n; ++i)
	{
		ll p,q,r;
		cin>>p>>q>>r;
		if(p==0)
		{
			cout<<"Finite\n";
			continue;
		}
		ll k=__gcd(p,q);
		p=p/k;
		q=q/k;
		while(q%r==0)
		{
			q=q/r;
		}
		k=__gcd(q,r);
		if(k==1)
			cout<<"Infinite\n";
		else
			cout<<"Finite\n";
	}
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll n;
	cin>>n;
	for (int i = 0; i < n; ++i)
	{
		ll p,q,r;
		cin>>p>>q>>r;
		if(p==0)
		{
			cout<<"Finite\n";
			continue;
		}
		ll k=__gcd(p,q);

		p=p/k;
		q=q/k;
		while(q%r==0)
		{
			q=q/r;
		}
		if(q==1)
		{
			cout<<"Finite\n";
			continue;
		}
		k=__gcd(q,r);

		if(k==1)
			cout<<"Infinite\n";
		else
			cout<<"Finite\n";
	}
}


#include<iostream>
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
	int n;
	cin>>n;
	for (int i = 0; i < n; ++i)
	{
		ll p,q,r;
		cin>>p>>q>>r;
		if(p==0)
		{
			cout<<"Finite\n";
			continue;
		}
		ll k=__gcd(p,q);
		p=p/k;
		q=q/k;
		while(q%r==0)
			q=q/r;
		k=__gcd(q,r);
		while(k!=1)
		{
			q=q/k;
			k=__gcd(q,r);
		}
		if(q==1)
			cout<<"Finite\n";
		else
			cout<<"Infinite\n";
	}
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp> //required
#define pb push_back
#define pf push_front
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define sz(x)  (long long)(x).size()
#define sqr(x) (x)*(x)
#define sof sizeof
#define resz resize
#define ins insert
#define lowerB lower_bound
#define upperB upper_bound
#define mkp make_pair
#define mkt make_tuple
#define act(a,v) (get<(long long)a>(v))
#define mod 1000000007
#define mod1 998244353
#define N 500003  //N idhar hai
#define inf 1e18
#define minf -1e18
#define min3(a,b,c) min(a,min(b,c));
#define max3(a,b,c) max(a,max(b,c));
#define Foreach(i, c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define rof(i,a,b) for(int (i)=(a);(i) >= (b); --(i))
#define rep(i, c) for(auto &(i) : (c))
#define sumall(v) accumulate(all(v),(ll)0)
#define suma accumulate
#define FastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PRECISION(n) std::cout.unsetf ( std::ios::floatfield );std::cout.precision((ll)n)
#define FileIO freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
#define call(dr) dr.pb(mkp(1,0));dr.pb(mkp(-1,0));dr.pb(mkp(0,1));dr.pb(mkp(0,-1))
#define fill(dp,a,b) for(int i=0;i<a;i++)for(int j=0;j<b;j++)dp[i][j]=-1;
#define PI M_PI
#define fi first
#define se second
#define here cout<<"RSSB"<<endl
#define showzero cout<<std::showpoint
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) ((a*b)/gcd(a,b))
#define bitcount( x ) __builtin_popcountll( x )
#define ls(i) (1ll<<(i))
#define rs(i) ((i)>>1)
#define br "\n"
#define sp " "
using namespace __gnu_pbds; //required
using namespace  std ;
typedef double db;
typedef long long ll ;
typedef vector<int>vi ;
//typedef vector<l>vl ;
typedef vector<ll>vll ;
typedef vector<vector<ll> >vvl ;
typedef pair<int,int> pii ;
typedef pair<ll,ll> pll ;
typedef vector<pll>vpll;
typedef vector<string>vstr;
typedef vector<bool>vbool ;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
void READ(vll &v, ll n){ll a ;for(ll i=0 ; i<n ; i=i+1){cin>>a ;v.pb(a);}}
void PRINT(vll &v , ll a = 0){for(ll i=a ; i<v.size() ; i=i+1){/*v[i] = max((ll)0,v[i]);*/cout<<v[i]<<" " ;}cout<<"\n" ;}
double logy(ll n , ll b){if(b==0){return(-1);}if(n==1){return(0);}return((db)log10(n)/log10(b));}
long long power(long long k , long long n , long long m=mod){long long res = 1;while(n){if(n%2!=0){res = (res*k)%m ;}k = (k*k)%m ;n = n/2 ;}return(res) ;}
double power(double k , long long n ){double res = 1;while(n){if(n%2!=0){res = (res*k) ;}k = (k*k) ;n = n/2 ;}return(res) ;}
ll sign(ll i){if(i==0){return(0);}if(i>0){return(1);}return(-1);}
double distance(pll a , pll b){return((db)(sqrt( sqr(a.fi-b.fi) + sqr(a.se-b.se))));}
double distance(pair<ll,pll> a , pair<ll,pll> b){return((db)(sqrt( sqr(a.fi-b.fi) + sqr(a.se.fi-b.se.fi) + sqr(a.se.se - b.se.se))));}
const ll dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const ll dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
vpll dr;
ll n,m,k,h,a1,a2;
db prob;
string str;
bool ok = true;
int main()
{
    FastIO ;
    PRECISION(10) ;
    ll a,b,c,d,i,j,t,x,y,z;//,x1,y1,z1,e,f;
    db p,q,r,u,w ;
    char ch,chr ;
    string st,str1,str2,str3,str4;
    //ll a1=0,a2=0,b1=0,b2=0,c1=0 ;
    //cin>>str>>str1;
    //Listen to Zero(Imagine Dragons!!)
    //Stay Motivated!!
    //Never use i for two or more nested loops!!
    //Range Query + Constructive + DSU +  Graph Theory + Math + DP + Bitmasks + Geometry + Brute Force + Greedy + Implementation + Binary Search
    //call(dr);
    cin>>t;
    while(t--)
    {
        cin>>a>>b>>c;
        d = gcd(a,b);
        a/=d;
        b/=d;
        ok = false;
        x = c;
        for(i=c ; i<inf ; i*=c)
        {
            //cout<<i<<" ";
            if(i%b==0)
            {
                //cout<<b<<" "<<i<<"\n";
                ok = true;
                break;
            }
            if(i>inf/c)
            {
                break;
            }
        }
        if(ok)
        {
            cout<<"Finite\n";
        }
        else
        {
            cout<<"Infinite\n";
        }
    }
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp> //required
#define pb push_back
#define pf push_front
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define sz(x)  (long long)(x).size()
#define sqr(x) (x)*(x)
#define sof sizeof
#define resz resize
#define ins insert
#define lowerB lower_bound
#define upperB upper_bound
#define mkp make_pair
#define mkt make_tuple
#define act(a,v) (get<(long long)a>(v))
#define mod 1000000007
#define mod1 998244353
#define N 500003  //N idhar hai
#define inf 1e18
#define minf -1e18
#define min3(a,b,c) min(a,min(b,c));
#define max3(a,b,c) max(a,max(b,c));
#define Foreach(i, c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define rof(i,a,b) for(int (i)=(a);(i) >= (b); --(i))
#define rep(i, c) for(auto &(i) : (c))
#define sumall(v) accumulate(all(v),(ll)0)
#define suma accumulate
#define FastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PRECISION(n) std::cout.unsetf ( std::ios::floatfield );std::cout.precision((ll)n)
#define FileIO freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
#define call(dr) dr.pb(mkp(1,0));dr.pb(mkp(-1,0));dr.pb(mkp(0,1));dr.pb(mkp(0,-1))
#define fill(dp,a,b) for(int i=0;i<a;i++)for(int j=0;j<b;j++)dp[i][j]=-1;
#define PI M_PI
#define fi first
#define se second
#define here cout<<"RSSB"<<endl
#define showzero cout<<std::showpoint
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) ((a*b)/gcd(a,b))
#define bitcount( x ) __builtin_popcountll( x )
#define ls(i) (1ll<<(i))
#define rs(i) ((i)>>1)
#define br "\n"
#define sp " "
using namespace __gnu_pbds; //required
using namespace  std ;
typedef double db;
typedef long long ll ;
typedef vector<int>vi ;
//typedef vector<l>vl ;
typedef vector<ll>vll ;
typedef vector<vector<ll> >vvl ;
typedef pair<int,int> pii ;
typedef pair<ll,ll> pll ;
typedef vector<pll>vpll;
typedef vector<string>vstr;
typedef vector<bool>vbool ;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
void READ(vll &v, ll n){ll a ;for(ll i=0 ; i<n ; i=i+1){cin>>a ;v.pb(a);}}
void PRINT(vll &v , ll a = 0){for(ll i=a ; i<v.size() ; i=i+1){/*v[i] = max((ll)0,v[i]);*/cout<<v[i]<<" " ;}cout<<"\n" ;}
double logy(ll n , ll b){if(b==0){return(-1);}if(n==1){return(0);}return((db)log10(n)/log10(b));}
long long power(long long k , long long n , long long m=mod){long long res = 1;while(n){if(n%2!=0){res = (res*k)%m ;}k = (k*k)%m ;n = n/2 ;}return(res) ;}
double power(double k , long long n ){double res = 1;while(n){if(n%2!=0){res = (res*k) ;}k = (k*k) ;n = n/2 ;}return(res) ;}
ll sign(ll i){if(i==0){return(0);}if(i>0){return(1);}return(-1);}
double distance(pll a , pll b){return((db)(sqrt( sqr(a.fi-b.fi) + sqr(a.se-b.se))));}
double distance(pair<ll,pll> a , pair<ll,pll> b){return((db)(sqrt( sqr(a.fi-b.fi) + sqr(a.se.fi-b.se.fi) + sqr(a.se.se - b.se.se))));}
const ll dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const ll dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
vpll dr;
ll n,m,k,h,a1,a2;
db prob;
string str;
bool ok = true;
int main()
{
    FastIO ;
    PRECISION(10) ;
    ll a,b,c,d,i,j,t,x,y,z;//,x1,y1,z1,e,f;
    db p,q,r,u,w ;
    char ch,chr ;
    string st,str1,str2,str3,str4;
    //ll a1=0,a2=0,b1=0,b2=0,c1=0 ;
    //cin>>str>>str1;
    //Listen to Zero(Imagine Dragons!!)
    //Stay Motivated!!
    //Never use i for two or more nested loops!!
    //Range Query + Constructive + DSU +  Graph Theory + Math + DP + Bitmasks + Geometry + Brute Force + Greedy + Implementation + Binary Search
    //call(dr);
    cin>>t;
    while(t--)
    {
        cin>>a>>b>>c;
        d = gcd(a,b);
        a/=d;
        b/=d;
        if(b<=c)
        {
            if(c%b)
            {
                cout<<"Infinite\n";
            }
            else
            {
                cout<<"Finite\n";
            }
        }
        else
        {
            d = logy(b,c);
            if(pow(c,d)==b)
            {
                cout<<"Finite\n";
            }
            else
            {
                cout<<"Infinite\n";
            }
        }
    }
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp> //required
#define pb push_back
#define pf push_front
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define sz(x)  (long long)(x).size()
#define sqr(x) (x)*(x)
#define sof sizeof
#define resz resize
#define ins insert
#define lowerB lower_bound
#define upperB upper_bound
#define mkp make_pair
#define mkt make_tuple
#define act(a,v) (get<(long long)a>(v))
#define mod 1000000007
#define mod1 998244353
#define N 500003  //N idhar hai
#define inf 8e18
#define minf -1e18
#define min3(a,b,c) min(a,min(b,c));
#define max3(a,b,c) max(a,max(b,c));
#define Foreach(i, c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define rof(i,a,b) for(int (i)=(a);(i) >= (b); --(i))
#define rep(i, c) for(auto &(i) : (c))
#define sumall(v) accumulate(all(v),(ll)0)
#define suma accumulate
#define FastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PRECISION(n) std::cout.unsetf ( std::ios::floatfield );std::cout.precision((ll)n)
#define FileIO freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
#define call(dr) dr.pb(mkp(1,0));dr.pb(mkp(-1,0));dr.pb(mkp(0,1));dr.pb(mkp(0,-1))
#define fill(dp,a,b) for(int i=0;i<a;i++)for(int j=0;j<b;j++)dp[i][j]=-1;
#define PI M_PI
#define fi first
#define se second
#define here cout<<"RSSB"<<endl
#define showzero cout<<std::showpoint
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) ((a*b)/gcd(a,b))
#define bitcount( x ) __builtin_popcountll( x )
#define ls(i) (1ll<<(i))
#define rs(i) ((i)>>1)
#define br "\n"
#define sp " "
using namespace __gnu_pbds; //required
using namespace  std ;
typedef double db;
typedef long long ll ;
typedef vector<int>vi ;
//typedef vector<l>vl ;
typedef vector<ll>vll ;
typedef vector<vector<ll> >vvl ;
typedef pair<int,int> pii ;
typedef pair<ll,ll> pll ;
typedef vector<pll>vpll;
typedef vector<string>vstr;
typedef vector<bool>vbool ;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
void READ(vll &v, ll n){ll a ;for(ll i=0 ; i<n ; i=i+1){cin>>a ;v.pb(a);}}
void PRINT(vll &v , ll a = 0){for(ll i=a ; i<v.size() ; i=i+1){/*v[i] = max((ll)0,v[i]);*/cout<<v[i]<<" " ;}cout<<"\n" ;}
double logy(ll n , ll b){if(b==0){return(-1);}if(n==1){return(0);}return((db)log10(n)/log10(b));}
long long power(long long k , long long n , long long m=mod){long long res = 1;while(n){if(n%2!=0){res = (res*k)%m ;}k = (k*k)%m ;n = n/2 ;}return(res) ;}
double power(double k , long long n ){double res = 1;while(n){if(n%2!=0){res = (res*k) ;}k = (k*k) ;n = n/2 ;}return(res) ;}
ll sign(ll i){if(i==0){return(0);}if(i>0){return(1);}return(-1);}
double distance(pll a , pll b){return((db)(sqrt( sqr(a.fi-b.fi) + sqr(a.se-b.se))));}
double distance(pair<ll,pll> a , pair<ll,pll> b){return((db)(sqrt( sqr(a.fi-b.fi) + sqr(a.se.fi-b.se.fi) + sqr(a.se.se - b.se.se))));}
const ll dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const ll dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
vpll dr;
ll n,m,k,h,a1,a2;
db prob;
string str;
bool ok = true;
int main()
{
    FastIO ;
    PRECISION(10) ;
    ll a,b,c,d,i,j,t,x,y,z;//,x1,y1,z1,e,f;
    db p,q,r,u,w ;
    char ch,chr ;
    string st,str1,str2,str3,str4;
    //ll a1=0,a2=0,b1=0,b2=0,c1=0 ;
    //cin>>str>>str1;
    //Listen to Zero(Imagine Dragons!!)
    //Stay Motivated!!
    //Never use i for two or more nested loops!!
    //Range Query + Constructive + DSU +  Graph Theory + Math + DP + Bitmasks + Geometry + Brute Force + Greedy + Implementation + Binary Search
    //call(dr);
    cin>>t;
    while(t--)
    {
        cin>>a>>b>>c;
        d = gcd(a,b);
        a/=d;
        b/=d;
        ok = false;
        x = c;
        for(i=c ;  ; i*=c)
        {
            //cout<<i<<" ";
            if(i%b==0)
            {
                //cout<<b<<" "<<i<<"\n";
                ok = true;
                break;
            }
            if(i>inf/c)
            {
                break;
            }
        }
        if(ok)
        {
            cout<<"Finite\n";
        }
        else
        {
            cout<<"Infinite\n";
        }
    }
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp> //required
#define pb push_back
#define pf push_front
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define sz(x)  (long long)(x).size()
#define sqr(x) (x)*(x)
#define sof sizeof
#define resz resize
#define ins insert
#define lowerB lower_bound
#define upperB upper_bound
#define mkp make_pair
#define mkt make_tuple
#define act(a,v) (get<(long long)a>(v))
#define mod 1000000007
#define mod1 998244353
#define N 500003  //N idhar hai
#define inf 1e18
#define minf -1e18
#define min3(a,b,c) min(a,min(b,c));
#define max3(a,b,c) max(a,max(b,c));
#define Foreach(i, c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define rof(i,a,b) for(int (i)=(a);(i) >= (b); --(i))
#define rep(i, c) for(auto &(i) : (c))
#define sumall(v) accumulate(all(v),(ll)0)
#define suma accumulate
#define FastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PRECISION(n) std::cout.unsetf ( std::ios::floatfield );std::cout.precision((ll)n)
#define FileIO freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
#define call(dr) dr.pb(mkp(1,0));dr.pb(mkp(-1,0));dr.pb(mkp(0,1));dr.pb(mkp(0,-1))
#define fill(dp,a,b) for(int i=0;i<a;i++)for(int j=0;j<b;j++)dp[i][j]=-1;
#define PI M_PI
#define fi first
#define se second
#define here cout<<"RSSB"<<endl
#define showzero cout<<std::showpoint
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) ((a*b)/gcd(a,b))
#define bitcount( x ) __builtin_popcountll( x )
#define ls(i) (1ll<<(i))
#define rs(i) ((i)>>1)
#define br "\n"
#define sp " "
using namespace __gnu_pbds; //required
using namespace  std ;
typedef double db;
typedef long long ll ;
typedef vector<int>vi ;
//typedef vector<l>vl ;
typedef vector<ll>vll ;
typedef vector<vector<ll> >vvl ;
typedef pair<int,int> pii ;
typedef pair<ll,ll> pll ;
typedef vector<pll>vpll;
typedef vector<string>vstr;
typedef vector<bool>vbool ;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
void READ(vll &v, ll n){ll a ;for(ll i=0 ; i<n ; i=i+1){cin>>a ;v.pb(a);}}
void PRINT(vll &v , ll a = 0){for(ll i=a ; i<v.size() ; i=i+1){/*v[i] = max((ll)0,v[i]);*/cout<<v[i]<<" " ;}cout<<"\n" ;}
double logy(ll n , ll b){if(b==0){return(-1);}if(n==1){return(0);}return((db)log10(n)/log10(b));}
long long power(long long k , long long n , long long m=mod){long long res = 1;while(n){if(n%2!=0){res = (res*k)%m ;}k = (k*k)%m ;n = n/2 ;}return(res) ;}
double power(double k , long long n ){double res = 1;while(n){if(n%2!=0){res = (res*k) ;}k = (k*k) ;n = n/2 ;}return(res) ;}
ll sign(ll i){if(i==0){return(0);}if(i>0){return(1);}return(-1);}
double distance(pll a , pll b){return((db)(sqrt( sqr(a.fi-b.fi) + sqr(a.se-b.se))));}
double distance(pair<ll,pll> a , pair<ll,pll> b){return((db)(sqrt( sqr(a.fi-b.fi) + sqr(a.se.fi-b.se.fi) + sqr(a.se.se - b.se.se))));}
const ll dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const ll dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
vpll dr;
ll n,m,k,h,a1,a2;
db prob;
string str;
bool ok = true;
int main()
{
    FastIO ;
    PRECISION(10) ;
    ll a,b,c,d,i,j,t,x,y,z;//,x1,y1,z1,e,f;
    db p,q,r,u,w ;
    char ch,chr ;
    string st,str1,str2,str3,str4;
    //ll a1=0,a2=0,b1=0,b2=0,c1=0 ;
    //cin>>str>>str1;
    //Listen to Zero(Imagine Dragons!!)
    //Stay Motivated!!
    //Never use i for two or more nested loops!!
    //Range Query + Constructive + DSU +  Graph Theory + Math + DP + Bitmasks + Geometry + Brute Force + Greedy + Implementation + Binary Search
    //call(dr);
    cin>>t;
    while(t--)
    {
        cin>>a>>b>>c;
        d = gcd(a,b);
        a/=d;
        b/=d;
        ok = false;
        x = c;
        for(i=c ; i<=inf/c ; i*=c)
        {
            if(i%b==0)
            {
                //cout<<b<<" "<<i<<"\n";
                ok = true;
                break;
            }
        }
        if(ok)
        {
            cout<<"Finite\n";
        }
        else
        {
            cout<<"Infinite\n";
        }
    }
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp> //required
#define pb push_back
#define pf push_front
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define sz(x)  (long long)(x).size()
#define sqr(x) (x)*(x)
#define sof sizeof
#define resz resize
#define ins insert
#define lowerB lower_bound
#define upperB upper_bound
#define mkp make_pair
#define mkt make_tuple
#define act(a,v) (get<(long long)a>(v))
#define mod 1000000007
#define mod1 998244353
#define N 500003  //N idhar hai
#define inf 1e18
#define minf -1e18
#define min3(a,b,c) min(a,min(b,c));
#define max3(a,b,c) max(a,max(b,c));
#define Foreach(i, c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define rof(i,a,b) for(int (i)=(a);(i) >= (b); --(i))
#define rep(i, c) for(auto &(i) : (c))
#define sumall(v) accumulate(all(v),(ll)0)
#define suma accumulate
#define FastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PRECISION(n) std::cout.unsetf ( std::ios::floatfield );std::cout.precision((ll)n)
#define FileIO freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
#define call(dr) dr.pb(mkp(1,0));dr.pb(mkp(-1,0));dr.pb(mkp(0,1));dr.pb(mkp(0,-1))
#define fill(dp,a,b) for(int i=0;i<a;i++)for(int j=0;j<b;j++)dp[i][j]=-1;
#define PI M_PI
#define fi first
#define se second
#define here cout<<"RSSB"<<endl
#define showzero cout<<std::showpoint
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) ((a*b)/gcd(a,b))
#define bitcount( x ) __builtin_popcountll( x )
#define ls(i) (1ll<<(i))
#define rs(i) ((i)>>1)
#define br "\n"
#define sp " "
using namespace __gnu_pbds; //required
using namespace  std ;
typedef double db;
typedef long long ll ;
typedef vector<int>vi ;
//typedef vector<l>vl ;
typedef vector<ll>vll ;
typedef vector<vector<ll> >vvl ;
typedef pair<int,int> pii ;
typedef pair<ll,ll> pll ;
typedef vector<pll>vpll;
typedef vector<string>vstr;
typedef vector<bool>vbool ;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
void READ(vll &v, ll n){ll a ;for(ll i=0 ; i<n ; i=i+1){cin>>a ;v.pb(a);}}
void PRINT(vll &v , ll a = 0){for(ll i=a ; i<v.size() ; i=i+1){/*v[i] = max((ll)0,v[i]);*/cout<<v[i]<<" " ;}cout<<"\n" ;}
double logy(ll n , ll b){if(b==0){return(-1);}if(n==1){return(0);}return((db)log10(n)/log10(b));}
long long power(long long k , long long n , long long m=mod){long long res = 1;while(n){if(n%2!=0){res = (res*k)%m ;}k = (k*k)%m ;n = n/2 ;}return(res) ;}
double power(double k , long long n ){double res = 1;while(n){if(n%2!=0){res = (res*k) ;}k = (k*k) ;n = n/2 ;}return(res) ;}
ll sign(ll i){if(i==0){return(0);}if(i>0){return(1);}return(-1);}
double distance(pll a , pll b){return((db)(sqrt( sqr(a.fi-b.fi) + sqr(a.se-b.se))));}
double distance(pair<ll,pll> a , pair<ll,pll> b){return((db)(sqrt( sqr(a.fi-b.fi) + sqr(a.se.fi-b.se.fi) + sqr(a.se.se - b.se.se))));}
const ll dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const ll dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
vpll dr;
ll n,m,k,h,a1,a2;
db prob;
string str;
bool ok = true;
int main()
{
    FastIO ;
    PRECISION(10) ;
    ll a,b,c,d,i,j,t,x,y,z;//,x1,y1,z1,e,f;
    db p,q,r,u,w ;
    char ch,chr ;
    string st,str1,str2,str3,str4;
    //ll a1=0,a2=0,b1=0,b2=0,c1=0 ;
    //cin>>str>>str1;
    //Listen to Zero(Imagine Dragons!!)
    //Stay Motivated!!
    //Never use i for two or more nested loops!!
    //Range Query + Constructive + DSU +  Graph Theory + Math + DP + Bitmasks + Geometry + Brute Force + Greedy + Implementation + Binary Search
    //call(dr);
    cin>>t;
    while(t--)
    {
        cin>>a>>b>>c;
        d = gcd(a,b);
        a/=d;
        b/=d;
        ok = false;
        x = c;
        for(i=c ; i<inf ; i*=c)
        {
            if(i%b==0)
            {
                ok = true;
                break;
            }
        }
        if(ok)
        {
            cout<<"Finite\n";
        }
        else
        {
            cout<<"Infinite\n";
        }
    }
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp> //required
#define pb push_back
#define pf push_front
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define sz(x)  (long long)(x).size()
#define sqr(x) (x)*(x)
#define sof sizeof
#define resz resize
#define ins insert
#define lowerB lower_bound
#define upperB upper_bound
#define mkp make_pair
#define mkt make_tuple
#define act(a,v) (get<(long long)a>(v))
#define mod 1000000007
#define mod1 998244353
#define N 500003  //N idhar hai
#define inf 8e18
#define minf -1e18
#define min3(a,b,c) min(a,min(b,c));
#define max3(a,b,c) max(a,max(b,c));
#define Foreach(i, c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define rof(i,a,b) for(int (i)=(a);(i) >= (b); --(i))
#define rep(i, c) for(auto &(i) : (c))
#define sumall(v) accumulate(all(v),(ll)0)
#define suma accumulate
#define FastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PRECISION(n) std::cout.unsetf ( std::ios::floatfield );std::cout.precision((ll)n)
#define FileIO freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
#define call(dr) dr.pb(mkp(1,0));dr.pb(mkp(-1,0));dr.pb(mkp(0,1));dr.pb(mkp(0,-1))
#define fill(dp,a,b) for(int i=0;i<a;i++)for(int j=0;j<b;j++)dp[i][j]=-1;
#define PI M_PI
#define fi first
#define se second
#define here cout<<"RSSB"<<endl
#define showzero cout<<std::showpoint
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) ((a*b)/gcd(a,b))
#define bitcount( x ) __builtin_popcountll( x )
#define ls(i) (1ll<<(i))
#define rs(i) ((i)>>1)
#define br "\n"
#define sp " "
using namespace __gnu_pbds; //required
using namespace  std ;
typedef double db;
typedef long long ll ;
typedef vector<int>vi ;
//typedef vector<l>vl ;
typedef vector<ll>vll ;
typedef vector<vector<ll> >vvl ;
typedef pair<int,int> pii ;
typedef pair<ll,ll> pll ;
typedef vector<pll>vpll;
typedef vector<string>vstr;
typedef vector<bool>vbool ;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
void READ(vll &v, ll n){ll a ;for(ll i=0 ; i<n ; i=i+1){cin>>a ;v.pb(a);}}
void PRINT(vll &v , ll a = 0){for(ll i=a ; i<v.size() ; i=i+1){/*v[i] = max((ll)0,v[i]);*/cout<<v[i]<<" " ;}cout<<"\n" ;}
double logy(ll n , ll b){if(b==0){return(-1);}if(n==1){return(0);}return((db)log10(n)/log10(b));}
long long power(long long k , long long n , long long m=mod){long long res = 1;while(n){if(n%2!=0){res = (res*k)%m ;}k = (k*k)%m ;n = n/2 ;}return(res) ;}
double power(double k , long long n ){double res = 1;while(n){if(n%2!=0){res = (res*k) ;}k = (k*k) ;n = n/2 ;}return(res) ;}
ll sign(ll i){if(i==0){return(0);}if(i>0){return(1);}return(-1);}
double distance(pll a , pll b){return((db)(sqrt( sqr(a.fi-b.fi) + sqr(a.se-b.se))));}
double distance(pair<ll,pll> a , pair<ll,pll> b){return((db)(sqrt( sqr(a.fi-b.fi) + sqr(a.se.fi-b.se.fi) + sqr(a.se.se - b.se.se))));}
const ll dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const ll dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
vpll dr;
ll n,m,k,h,a1,a2;
db prob;
string str;
bool ok = true;
int main()
{
    FastIO ;
    PRECISION(10) ;
    ll a,b,c,d,i,j,t,x,y,z;//,x1,y1,z1,e,f;
    db p,q,r,u,w ;
    char ch,chr ;
    string st,str1,str2,str3,str4;
    //ll a1=0,a2=0,b1=0,b2=0,c1=0 ;
    //cin>>str>>str1;
    //Listen to Zero(Imagine Dragons!!)
    //Stay Motivated!!
    //Never use i for two or more nested loops!!
    //Range Query + Constructive + DSU +  Graph Theory + Math + DP + Bitmasks + Geometry + Brute Force + Greedy + Implementation + Binary Search
    //call(dr);
    cin>>t;
    while(t--)
    {
        cin>>a>>b>>c;
        d = gcd(a,b);
        a/=d;
        b/=d;
        ok = false;
        x = gcd(c,b);
        //cout<<x<<"\n";
        while(x!=1 && b%x==0)
        {
            b/=x;
            if(b%x)
            {
                x = gcd(b,x);
            }
        }
        if(b==1)
        {
            cout<<"Finite\n";
        }
        else
        {
            cout<<"Infinite\n";
        }
    }
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp> //required
#define pb push_back
#define pf push_front
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define sz(x)  (long long)(x).size()
#define sqr(x) (x)*(x)
#define sof sizeof
#define resz resize
#define ins insert
#define lowerB lower_bound
#define upperB upper_bound
#define mkp make_pair
#define mkt make_tuple
#define act(a,v) (get<(long long)a>(v))
#define mod 1000000007
#define mod1 998244353
#define N 500003  //N idhar hai
#define inf 8e18
#define minf -1e18
#define min3(a,b,c) min(a,min(b,c));
#define max3(a,b,c) max(a,max(b,c));
#define Foreach(i, c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define rof(i,a,b) for(int (i)=(a);(i) >= (b); --(i))
#define rep(i, c) for(auto &(i) : (c))
#define sumall(v) accumulate(all(v),(ll)0)
#define suma accumulate
#define FastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PRECISION(n) std::cout.unsetf ( std::ios::floatfield );std::cout.precision((ll)n)
#define FileIO freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
#define call(dr) dr.pb(mkp(1,0));dr.pb(mkp(-1,0));dr.pb(mkp(0,1));dr.pb(mkp(0,-1))
#define fill(dp,a,b) for(int i=0;i<a;i++)for(int j=0;j<b;j++)dp[i][j]=-1;
#define PI M_PI
#define fi first
#define se second
#define here cout<<"RSSB"<<endl
#define showzero cout<<std::showpoint
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) ((a*b)/gcd(a,b))
#define bitcount( x ) __builtin_popcountll( x )
#define ls(i) (1ll<<(i))
#define rs(i) ((i)>>1)
#define br "\n"
#define sp " "
using namespace __gnu_pbds; //required
using namespace  std ;
typedef double db;
typedef long long ll ;
typedef vector<int>vi ;
//typedef vector<l>vl ;
typedef vector<ll>vll ;
typedef vector<vector<ll> >vvl ;
typedef pair<int,int> pii ;
typedef pair<ll,ll> pll ;
typedef vector<pll>vpll;
typedef vector<string>vstr;
typedef vector<bool>vbool ;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
void READ(vll &v, ll n){ll a ;for(ll i=0 ; i<n ; i=i+1){cin>>a ;v.pb(a);}}
void PRINT(vll &v , ll a = 0){for(ll i=a ; i<v.size() ; i=i+1){/*v[i] = max((ll)0,v[i]);*/cout<<v[i]<<" " ;}cout<<"\n" ;}
double logy(ll n , ll b){if(b==0){return(-1);}if(n==1){return(0);}return((db)log10(n)/log10(b));}
long long power(long long k , long long n , long long m=mod){long long res = 1;while(n){if(n%2!=0){res = (res*k)%m ;}k = (k*k)%m ;n = n/2 ;}return(res) ;}
double power(double k , long long n ){double res = 1;while(n){if(n%2!=0){res = (res*k) ;}k = (k*k) ;n = n/2 ;}return(res) ;}
ll sign(ll i){if(i==0){return(0);}if(i>0){return(1);}return(-1);}
double distance(pll a , pll b){return((db)(sqrt( sqr(a.fi-b.fi) + sqr(a.se-b.se))));}
double distance(pair<ll,pll> a , pair<ll,pll> b){return((db)(sqrt( sqr(a.fi-b.fi) + sqr(a.se.fi-b.se.fi) + sqr(a.se.se - b.se.se))));}
const ll dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const ll dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
vpll dr;
ll n,m,k,h,a1,a2;
db prob;
string str;
bool ok = true;
int main()
{
    FastIO ;
    PRECISION(10) ;
    ll a,b,c,d,i,j,t,x,y,z;//,x1,y1,z1,e,f;
    db p,q,r,u,w ;
    char ch,chr ;
    string st,str1,str2,str3,str4;
    //ll a1=0,a2=0,b1=0,b2=0,c1=0 ;
    //cin>>str>>str1;
    //Listen to Zero(Imagine Dragons!!)
    //Stay Motivated!!
    //Never use i for two or more nested loops!!
    //Range Query + Constructive + DSU +  Graph Theory + Math + DP + Bitmasks + Geometry + Brute Force + Greedy + Implementation + Binary Search
    //call(dr);
    cin>>t;
    while(t--)
    {
        cin>>a>>b>>c;
        d = gcd(a,b);
        a/=d;
        b/=d;
        ok = false;
        x = gcd(c,b);
        //cout<<x<<"\n";
        while(x!=1 && b%x==0)
        {
            b/=x;
            x = gcd(b,c);
        }
        if(b==1)
        {
            cout<<"Finite\n";
        }
        else
        {
            cout<<"Infinite\n";
        }
    }
}


#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <sstream>
#include <cmath>
#include <stack>
#define LL unsigned long long
#define mod 1000000007
using namespace std;
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
bool solve(LL p, LL q, LL b){
    for(int i=1; i<=7; i++){
        b = (b%q*b%q)%q;
        if((p%q*b%q)%q == 0) return true;
    }
    return false;
}
int main(){
    int n;
    scanf("%d",&n);
    LL p,q,b;
    while(n--){
        scanf("%llu%llu%llu",&p,&q,&b);
        if(solve(p,q,b))
            printf("Finite\n");
        else printf("Infinite\n");
    }
}


#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <sstream>
#include <cmath>
#include <stack>
#define LL unsigned long long
#define mod 1000000007
using namespace std;
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
bool solve(LL p, LL q, LL b){
    for(int i=1; i<=6; i++){
        b = (b%q*b%q)%q;
        if((p%q*b%q)%q == 0) return true;
    }
    return false;
}
int main(){
    int n;
    scanf("%d",&n);
    LL p,q,b;
    while(n--){
        scanf("%llu%llu%llu",&p,&q,&b);
        if(solve(p,q,b))
            printf("Finite\n");
        else printf("Infinite\n");
    }
}


#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <sstream>
#include <cmath>
#include <stack>
#define LL long long
#define mod 1000000007
using namespace std;
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
bool solve(LL p, LL q, LL b){
    for(int i=1; i<=6; i++){
        b = (b*b)%q;
        if((p*b)%q == 0) return true;
    }
    return false;
}
int main(){
    int n;
    scanf("%d",&n);
    LL p,q,b;
    while(n--){
        scanf("%lld%lld%lld",&p,&q,&b);
        if(solve(p,q,b))
            printf("Finite\n");
        else printf("Infinite\n");
    }
}


#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <sstream>
#include <cmath>
#include <stack>
#define LL long long
#define mod 1000000007
using namespace std;
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
bool solve(LL p, LL q, LL b){
    for(int i=1; i<=7; i++){
        b = (b%q*b%q)%q;
        if((p%q*b%q)%q == 0) return true;
    }
    return false;
}
int main(){
    int n;
    scanf("%d",&n);
    LL p,q,b;
    while(n--){
        scanf("%lld%lld%lld",&p,&q,&b);
        if(solve(p,q,b))
            printf("Finite\n");
        else printf("Infinite\n");
    }
}


#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <sstream>
#include <cmath>
#include <stack>
#define LL long long
#define mod 1000000007
using namespace std;
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
typedef long long ll;
ll gcd(ll a,ll b)
{
    if (b == 0) return a;
    else return gcd(b,a%b);
}
int main()
{
    int n;
    scanf("%d",&n);
    ll p,q,b;
    while (n--)
    {
        scanf("%lld%lld%lld",&p,&q,&b);
        ll t = gcd(p,q);
        p /= t;
        q /= t;
        if (p == 0) puts("Finite");
        else
        {
            while (b % q)
            {
                ll g = gcd(b,q);
                if (g == 1) break;
                b = g;
                q /= g;
            }
            if (b % q == 0) puts("Finite");
            else puts("Infinite");
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <sstream>
#include <cmath>
#include <stack>
#define LL long long
#define mod 1000000007
using namespace std;
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
LL pow_mod(LL a, LL x, LL m){
    LL ans = 1;
    while(x){
        if(x & 1)
            ans = (ans * a) % m;
        x /= 2;
        a = (a * a) % m;
    }
    return ans;
}
bool solve(LL p, LL q, LL b){
    for(int i=1; i<=60; i++){
        LL ans = pow_mod(b,i,q);
        if((p*ans)%q == 0) return true;
    }
    return false;
}
int main(){
    int n;
    scanf("%d",&n);
    LL p,q,b;
    while(n--){
        scanf("%lld%lld%lld",&p,&q,&b);
        if(solve(p,q,b))
            printf("Finite\n");
        else printf("Infinite\n");
    }
}