// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
//int gcd(int a, int b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

ll m;
ll can (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	map <ll, bool> old;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (old[h]) return -1;
		old[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {
		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 || p2 < 1 || p1 == p2);// ans = -1;
			else {
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }
				//for (ll x = 1; ; x++) {
				//	//ans1 + x * p1 = ans2 + y * p2;
				//	ll a = ans1 + x * p1 - ans2;
				//	ll y = a / p2;
				//	if (y * p2 == a) {
				//		ans = ans1 + x * p1;
				//		break;
				//	}
				//	if (ans1 + x * p1 > ans2 + y * p2) break;
				//}
				ll x = ans1 - ans2;
				if (x < 0) { x = - x; swap (ans1, ans2); swap (p1, p2); }
				ll y = p1 - p2;
				if (y > 0 && x > y) {
					ans1 += (x/y) * p1;
					ans2 += (x/y) * p2;
				}

				const ll one = 1;
				map <ll, bool> old;
				while (ans1 != ans2) {
					ll x = ans1 - ans2;
					if (old[x]) { ans1 = -1; break; }
					old[x] = true;
					if (x < 0) x = -x;
					if (ans1 < ans2) ans1 += max (one, x / p1) * p1;
					else ans2 += max (one, x / p2) * p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

bool oldx[1000111];
ll m;
ll can (ll h, ll a, ll x, ll y) {
	static int canCnt = 0;
	canCnt++;
	ll ans = 0;
	For (i, 1000111) oldx[i] = false;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (oldx[h]) return -1;
		oldx[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {

		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 && p2 < 1) ans = -1;
			else {
				if (p1 < 1) p1 = 0;
				if (p2 < 1) p2 = 0;
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }

				ll cnter = 0;
				const ll one = 1;
				//map <ll, bool> old;
				while (ans1 != ans2) {
					cnter ++ ;
					ll x = ans1 - ans2;
					if (cnter > 90111000 )//|| old[x])
					{ ans1 = -1; break; }
					//old[x] = true;
					if (x < 0) x = -x;
					if (ans1 < ans2) ans1 += p1;
					else ans2 += p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

bool oldx[1000001];
ll m;
ll can (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	For (i, 1000001) oldx[i] = false;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (oldx[h]) return -1;
		oldx[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {

		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 && p2 < 1) ans = -1;
			else {
				if (p1 < 1) p1 = 0;
				if (p2 < 1) p2 = 0;

				ll cnter = 0;
				const ll one = 1;
				//map <ll, bool> old;
				while (ans1 != ans2) {
					cnter ++ ;
					ll x = ans1 - ans2;
					if (cnter > 2101000 )//|| old[x])
					{ ans1 = -1; break; }
					//old[x] = true;
					if (x < 0) x = -x;
					if (p1 != 0 && p2 != 0) {
						if (ans1 < ans2) ans1 += max (one, x / p1) * p1;
						else ans2 += max (one, x / p2) * p2;
					}
					else {
						if (ans1 < ans2) ans1 += p1;
						else ans2 += p2;
					}
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

ll m;
ll can (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	map <ll, bool> old;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (old[h]) return -1;
		old[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {
		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 || p2 < 1 || p1 == p2);// ans = -1;
			else {
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }
				//for (ll x = 1; ; x++) {
				//	//ans1 + x * p1 = ans2 + y * p2;
				//	ll a = ans1 + x * p1 - ans2;
				//	ll y = a / p2;
				//	if (y * p2 == a) {
				//		ans = ans1 + x * p1;
				//		break;
				//	}
				//	if (ans1 + x * p1 > ans2 + y * p2) break;
				//}
				ll kmm = p1 * p2 / gcd (p1, p2);
				ll mxa1  = ans1 + kmm, mxa2 = ans2 + kmm;

				ll cnter = 0;
				const ll one = 1;
				map <ll, bool> old;
				while (ans1 != ans2) {
					cnter ++ ;
					ll x = ans1 - ans2;
					if (cnter > 500111 || old[x] || ans1 > mxa1 || ans2 > mxa2) { ans1 = -1; break; }
					old[x] = true;
					if (x < 0) x = -x;
					if (ans1 < ans2) ans1 += max (one, x / p1) * p1;
					else ans2 += max (one, x / p2) * p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

ll m;
ll can (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	map <ll, bool> old;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (old[h]) return -1;
		old[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {
		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 || p2 < 1 || p1 == p2);// ans = -1;
			else {
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }
				//for (ll x = 1; ; x++) {
				//	//ans1 + x * p1 = ans2 + y * p2;
				//	ll a = ans1 + x * p1 - ans2;
				//	ll y = a / p2;
				//	if (y * p2 == a) {
				//		ans = ans1 + x * p1;
				//		break;
				//	}
				//	if (ans1 + x * p1 > ans2 + y * p2) break;
				//}
				ll kmm = p1 * p2 / gcd (p1, p2);
				ll mxa1  = ans1 + kmm, mxa2 = ans2 + kmm;

				const ll one = 1;
				map <ll, bool> old;
				while (ans1 != ans2) {
					ll x = ans1 - ans2;
					if (old[x] || ans1 > mxa1 || ans2 > mxa2) { ans1 = -1; break; }
					old[x] = true;
					if (x < 0) x = -x;
					if (ans1 < ans2) ans1 += max (one, x / p1) * p1;
					else ans2 += max (one, x / p2) * p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
int gcd(int a, int b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

ll m;
ll can (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	map <ll, bool> old;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (old[h]) return -1;
		old[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {
		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 || p2 < 1 || p1 == p2);// ans = -1;
			else {
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }
				//for (ll x = 1; ; x++) {
				//	//ans1 + x * p1 = ans2 + y * p2;
				//	ll a = ans1 + x * p1 - ans2;
				//	ll y = a / p2;
				//	if (y * p2 == a) {
				//		ans = ans1 + x * p1;
				//		break;
				//	}
				//	if (ans1 + x * p1 > ans2 + y * p2) break;
				//}
				ll one = 1;
				map <ll, bool> old;
				while (ans1 != ans2) {
					ll x = ans1 - ans2;
					if (old[x]) { ans1 = -1; break; }
					old[x] = true;
					if (ans1 < ans2) ans1 += min (one, (ans2-ans1) / p1) * p1;
					else ans2 += min (one, (ans1 - ans2) / p2) * p2;
				}
				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
int gcd(int a, int b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

ll m;
bool can (ll h, ll a, ll x, ll y) {
	map <ll, bool> old;
	while (h != a) {
		if (old[h]) {
			return false;
		}
		old[h] = true;
		h = x * h + y;
		h %= m;
	}
	return true;
}

/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	int ans = 0;

	if (can (h1, a1, x1, y1) && can (h2, a2, x2, y2)) {
		map <pll, bool> mp;
		while (h1 != a1 || h2 != a2) {
			ans++;
			if (mp[MP(h1,h2)] ) {
				ans = -1;
				break;
			}
			mp[MP(h1, h2)] = true;
			h1 = x1 * h1 + y1;
			h1 %= m;
			h2 = x2 * h2 + y2;
			h2 %= m;
		}
	}
	else ans = -1;

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

bool oldx[1000111];
ll m;
ll can (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	For (i, 1000111) oldx[i] = false;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (oldx[h]) return -1;
		oldx[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {

		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 && p2 < 1) ans = -1;
			else {
				if (p1 < 1) p1 = 0;
				if (p2 < 1) p2 = 0;

				ll cnter = 0;
				const ll one = 1;
				//map <ll, bool> old;
				while (ans1 != ans2) {
					cnter ++ ;
					ll x = ans1 - ans2;
					if (cnter > 2222000 )//|| old[x])
					{ ans1 = -1; break; }
					//old[x] = true;
					if (x < 0) x = -x;
					if (ans1 < ans2) ans1 += p1;
					else ans2 += p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
int gcd(int a, int b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

ll m;
bool can (ll h, ll a, ll x, ll y) {
	map <ll, bool> old;
	while (h != a) {
		if (old[h]) {
			return false;
		}
		old[h] = true;
		h = x * h + y;
		h %= m;
	}
	return true;
}
ll can2 (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	while (h != a) {
		ans++;
		h = x * h + y;
		h %= m;
	}
	return ans;
}
ll can3 (ll a, ll x, ll y) {
	ll ans = 0;
	ll h = a;
	map <ll, bool> old;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (old[h]) return -1;
		old[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	int ans = 0;

	if (can (h1, a1, x1, y1) && can (h2, a2, x2, y2)) {
		ll ans1 = can2 (h1, a1, x1, y1),
			ans2 = can2 (h2, a2, x2, y2);
		ll p1 = can3 (a1, x1, y1),
			p2 = can3 (a2, x2, y2);

		if (ans1 == ans2) ans = ans1;
		else {
			if (p1 == -1 || p2 == -1 || p1 == p2) ans = -1;
			else {
				ll x = ans2 - ans1;
				x /= (p1 - p2);
				//ans1 + x * p1 = ans2 + x * p2;
				//x * (p1 - p2) = ans2 - ans1;
				if (x > 0 && x * (p1 - p2) == ans2 - ans1) {
					ans = ans1 + x * p1;
				}
				else ans = -1;
			}
		}
	}
	else ans = -1;

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

bool oldx[1000111];
ll m;
ll can (ll h, ll a, ll x, ll y) {
	static int canCnt = 0;
	canCnt++;
	ll ans = 0;
	For (i, 1000111) oldx[i] = false;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (oldx[h]) return -1;
		oldx[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {

		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 && p2 < 1) ans = -1;
			else {
				if (p1 < 1) p1 = 0;
				if (p2 < 1) p2 = 0;
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }

				ll cnter = 0;
				const ll one = 1;
				map <ll, bool> old;
				while (ans1 != ans2) {
					cnter ++ ;
					ll x = ans1 - ans2;
					if (cnter > 2000111 || old[x])
					{ ans1 = -1; break; }
					old[x] = true;
					if (x < 0) x = -x;
					if (ans1 < ans2) ans1 += p1;
					else ans2 += p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

bool oldx[1000111];
ll m;
ll can (ll h, ll a, ll x, ll y) {
	static int canCnt = 0;
	canCnt++;
	ll ans = 0;
	For (i, 1000111) oldx[i] = false;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (oldx[h]) return -1;
		oldx[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {

		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 && p2 < 1) ans = -1;
			else {
				if (p1 < 1) p1 = 0;
				if (p2 < 1) p2 = 0;
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }

				//ll cnter = 0;
				const ll one = 1;
				map <ll, bool> old;
				while (ans1 != ans2) {
					//cnter ++ ;
					ll x = ans1 - ans2;
					if (old[x])
					{ ans1 = -1; break; }
					old[x] = true;
					if (x < 0) x = -x;
					if (ans1 < ans2) ans1 += p1;
					else ans2 += p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
int gcd(int a, int b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

ll m;
ll can (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	map <ll, bool> old;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (old[h]) return -1;
		old[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {
		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 || p2 < 1 || p1 == p2);// ans = -1;
			else {
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }
				//for (ll x = 1; ; x++) {
				//	//ans1 + x * p1 = ans2 + y * p2;
				//	ll a = ans1 + x * p1 - ans2;
				//	ll y = a / p2;
				//	if (y * p2 == a) {
				//		ans = ans1 + x * p1;
				//		break;
				//	}
				//	if (ans1 + x * p1 > ans2 + y * p2) break;
				//}
				const ll one = 1;
				map <ll, bool> old;
				while (ans1 != ans2) {
					ll x = ans1 - ans2;
					if (old[x]) { ans1 = -1; break; }
					old[x] = true;
					if (x < 0) x = -x;
					if (ans1 < ans2) ans1 += max (one, x / p1) * p1;
					else ans2 += max (one, x / p2) * p2;
				}
				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

bool oldx[1000001];
ll m;
ll can (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	For (i, 1000001) oldx[i] = false;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (oldx[h]) return -1;
		oldx[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {

		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 && p2 < 1) ans = -1;
			else {
				if (p1 < 1) p1 = 0;
				if (p2 < 1) p2 = 0;

				ll cnter = 0;
				const ll one = 1;
				while (ans1 != ans2) {
					cnter ++ ;
					ll x = ans1 - ans2;
					if (cnter > 2040000 )//|| old[x])
					{ ans1 = -1; break; }
					if (x < 0) x = -x;

					if (ans1 < ans2) ans1 += p1;
					else ans2 += p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <int, int> pii;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
int gcd(int a, int b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

/*_____________________________________________________________________________________*/
int main() {
	ll m;
	ll h, a, x, y;
	cin >> m
		>> h >> a >> x >> y;

	ll ans = -1, ans1 = 0, ans2 = 0;
	{
		map <ll, bool> old;
		while (h != a) {
			if (old[h]) {
				ans1 = -1;
				break;
			}
			ans1++;
			old[h] = true;
			h = x * h + y;
			h %= m;
		}
	}

	if (ans1 > 0) {
		cin	>> h >> a >> x >> y;
		map <ll, bool> old;
		while (h != a) {
			if (old[h]) {
				ans2 = ans1 = -1;
				break;
			}
			ans2++;
			old[h] = true;
			h = x * h + y;
			h %= m;
		}
	}

	if (ans1 > 0 && ans2 > 0) {
		ans = ans1 * ans2 / gcd (ans1, ans2);
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

ll m;
ll can (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	map <ll, bool> old;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (old[h]) return -1;
		old[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {
		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 || p2 < 1 || p1 == p2);// ans = -1;
			else {
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }
				//for (ll x = 1; ; x++) {
				//	//ans1 + x * p1 = ans2 + y * p2;
				//	ll a = ans1 + x * p1 - ans2;
				//	ll y = a / p2;
				//	if (y * p2 == a) {
				//		ans = ans1 + x * p1;
				//		break;
				//	}
				//	if (ans1 + x * p1 > ans2 + y * p2) break;
				//}
				ll kmm = p1 * p2 / gcd (p1, p2);
				ll mxa1  = ans1 + kmm, mxa2 = ans2 + kmm;

				ll cnter = 0;
				const ll one = 1;
				//map <ll, bool> old;
				while (ans1 != ans2) {
					cnter ++ ;
					ll x = ans1 - ans2;
					if (cnter > 111000 )//|| old[x] || ans1 > mxa1 || ans2 > mxa2)
					{ ans1 = -1; break; }
					//old[x] = true;
					if (x < 0) x = -x;
					if (ans1 < ans2) ans1 += max (one, x / p1) * p1;
					else ans2 += max (one, x / p2) * p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

ll m;
ll can (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	map <ll, bool> old;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (old[h]) return -1;
		old[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {
		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 || p2 < 1 || p1 == p2);// ans = -1;
			else {
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }
				//for (ll x = 1; ; x++) {
				//	//ans1 + x * p1 = ans2 + y * p2;
				//	ll a = ans1 + x * p1 - ans2;
				//	ll y = a / p2;
				//	if (y * p2 == a) {
				//		ans = ans1 + x * p1;
				//		break;
				//	}
				//	if (ans1 + x * p1 > ans2 + y * p2) break;
				//}
				ll kmm = p1 * p2 / gcd (p1, p2);
				ll mxa1  = ans1 + kmm, mxa2 = ans2 + kmm;

				ll cnter = 0;
				const ll one = 1;
				//map <ll, bool> old;
				while (ans1 != ans2) {
					cnter ++ ;
					ll x = ans1 - ans2;
					if (cnter > 20111000 )//|| old[x] || ans1 > mxa1 || ans2 > mxa2)
					{ ans1 = -1; break; }
					//old[x] = true;
					if (x < 0) x = -x;
					if (ans1 < ans2) ans1 += max (one, x / p1) * p1;
					else ans2 += max (one, x / p2) * p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
int gcd(int a, int b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

ll m;
ll can (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	map <ll, bool> old;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (old[h]) return -1;
		old[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {
		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 || p2 < 1 || p1 == p2);// ans = -1;
			else {
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }
				//for (ll x = 1; ; x++) {
				//	//ans1 + x * p1 = ans2 + y * p2;
				//	ll a = ans1 + x * p1 - ans2;
				//	ll y = a / p2;
				//	if (y * p2 == a) {
				//		ans = ans1 + x * p1;
				//		break;
				//	}
				//	if (ans1 + x * p1 > ans2 + y * p2) break;
				//}
				map <ll, bool> old;
				while (ans1 != ans2) {
					ll x = ans1 - ans2;
					if (old[x]) { ans1 = -1; break; }
					old[x] = true;
					if (ans1 < ans2) ans1 += p1;
					else ans2 += p2;
				}
				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

ll m;
ll can (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	map <ll, bool> old;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (old[h]) return -1;
		old[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {
		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 || p2 < 1 || p1 == p2);// ans = -1;
			else {
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }
				//for (ll x = 1; ; x++) {
				//	//ans1 + x * p1 = ans2 + y * p2;
				//	ll a = ans1 + x * p1 - ans2;
				//	ll y = a / p2;
				//	if (y * p2 == a) {
				//		ans = ans1 + x * p1;
				//		break;
				//	}
				//	if (ans1 + x * p1 > ans2 + y * p2) break;
				//}
				ll kmm = p1 * p2 / gcd (p1, p2);
				ll mxa1  = ans1 + kmm, mxa2 = ans2 + kmm;

				ll cnter = 0;
				const ll one = 1;
				//map <ll, bool> old;
				while (ans1 != ans2) {
					cnter ++ ;
					if (cnter > 1000 )//|| old[x] || ans1 > mxa1 || ans2 > mxa2)
					{ ans1 = -1; break; }
					ll x = ans1 - ans2;
					//old[x] = true;
					if (x < 0) x = -x;
					if (ans1 < ans2) ans1 += max (one, x / p1) * p1;
					else ans2 += max (one, x / p2) * p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
int gcd(int a, int b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

ll m;
ll can (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	map <ll, bool> old;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (old[h]) return -1;
		old[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {
		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 || p2 < 1 || p1 == p2);// ans = -1;
			else {
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }
				//for (ll x = 1; ; x++) {
				//	//ans1 + x * p1 = ans2 + y * p2;
				//	ll a = ans1 + x * p1 - ans2;
				//	ll y = a / p2;
				//	if (y * p2 == a) {
				//		ans = ans1 + x * p1;
				//		break;
				//	}
				//	if (ans1 + x * p1 > ans2 + y * p2) break;
				//}
				while (ans1 != ans2) {
					if (ans1 < ans2) ans1 += p1;
					else ans2 += p2;
				}
				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
int gcd(int a, int b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

ll m;
ll can (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	map <ll, bool> old;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (old[h]) return -1;
		old[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1;
	ll ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {
		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 || p2 < 1 || p1 == p2);// ans = -1;
			else {
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }
				for (ll x = 1; ; x++) {
					//ans1 + x * p1 = ans2 + y * p2;
					ll a = ans1 + x * p1 - ans2;
					ll y = a / p2;
					if (y * p2 == a) {
						ans = ans1 + x * p1;
						break;
					}
					if (ans1 + x * p1 > ans2 + y * p2) break;
				}
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

bool oldx[1000111];
ll m;
ll can (ll h, ll a, ll x, ll y) {
	static int canCnt = 0;
	canCnt++;
	ll ans = 0;
	For (i, 1000111) oldx[i] = false;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (oldx[h]) return -1;
		oldx[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {

		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 && p2 < 1) ans = -1;
			else {
				if (p1 < 1) p1 = 0;
				if (p2 < 1) p2 = 0;
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }

				ll cnter = 0;
				const ll one = 1;
				map <ll, bool> old;
				while (ans1 != ans2) {
					cnter ++ ;
					ll x = ans1 - ans2;
					if (cnter > 1000111 || old[x])
					{ ans1 = -1; break; }
					old[x] = true;
					if (x < 0) x = -x;
					if (ans1 < ans2) ans1 += p1;
					else ans2 += p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <int, int> pii;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
int gcd(ll a, ll b) {
	if (a == 0) return b;
	return (b==0)? a: gcd(b, a%b);
}
/*-------------------------------------------------------------------------------------*/

/*_____________________________________________________________________________________*/
int main() {
	ll m;
	ll h, a, x, y;
	cin >> m
		>> h >> a >> x >> y;

	ll ans = -1, ans1 = 0, ans2 = 0;
	{
		map <ll, bool> old;
		while (h != a) {
			if (old[h]) {
				ans1 = -1;
				break;
			}
			ans1++;
			old[h] = true;
			h = x * h + y;
			h %= m;
		}
	}

	if (ans1 > 0) {
		cin	>> h >> a >> x >> y;
		map <ll, bool> old;
		while (h != a) {
			if (old[h]) {
				ans2 = -1;
				break;
			}
			ans2++;
			old[h] = true;
			h = x * h + y;
			h %= m;
		}
	}

	if (ans1 > 0 && ans2 > 0) {
		ans = (ans1 * ans2) / gcd (ans1, ans2);
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

bool oldx[1000001];
ll m;
ll can (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	For (i, 1000001) oldx[i] = false;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (oldx[h]) return -1;
		oldx[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {

		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 && p2 < 1) ans = -1;
			else {
				if (p1 < 1) p1 = 0;
				if (p2 < 1) p2 = 0;

				ll cnter = 0;
				const ll one = 1;
				//map <ll, bool> old;
				while (ans1 != ans2) {
					cnter ++ ;
					ll x = ans1 - ans2;
					if (cnter > 1999000 )//|| old[x])
					{ ans1 = -1; break; }
					//old[x] = true;
					if (x < 0) x = -x;
					if (ans1 < ans2) ans1 += p1;
					else ans2 += p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <int, int> pii;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
int gcd(int a, int b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

const int sz = 100 * 1000;

/*_____________________________________________________________________________________*/
int main() {
	int m;
	int h, a, x, y;
	cin >> m
		>> h >> a >> x >> y;

	ll ans = -1, ans1 = 0, ans2 = 0;
	{
		map <int, bool> old;
		while (h != a) {
			if (old[h]) {
				ans1 = -1;
				break;
			}
			ans1++;
			old[h] = true;
			h = x * h + y;
			h %= m;
		}
	}

	if (ans1 > 0) {
		cin	>> h >> a >> x >> y;
		map <int, bool> old;
		while (h != a) {
			if (old[h]) {
				ans2 = ans1 = -1;
				break;
			}
			ans2++;
			old[h] = true;
			h = x * h + y;
			h %= m;
		}
	}

	if (ans1 > 0 && ans2 > 0) {
		ans = ans1 * ans2 / gcd (ans1, ans2);
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

bool oldx[1000001];
ll m;
ll can (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	For (i, 1000001) oldx[i] = false;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (oldx[h]) return -1;
		oldx[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {

		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 && p2 < 1) ans = -1;
			else {
				if (p1 < 1) p1 = 0;
				if (p2 < 1) p2 = 0;

				ll cnter = 0;
				const ll one = 1;
				if (p1 != 0 && p2 != 0) {
					while (ans1 != ans2) {
						cnter ++ ;
						ll x = ans1 - ans2;
						if (cnter > 2101000 )//|| old[x])
						{ ans1 = -1; break; }
						if (x < 0) x = -x;

						if (ans1 < ans2) ans1 += max (one, x / p1) * p1;
						else ans2 += max (one, x / p2) * p2;
					}
				}
				else {
					while (ans1 != ans2) {
						cnter ++ ;
						ll x = ans1 - ans2;
						if (cnter > 2101000 )//|| old[x])
						{ ans1 = -1; break; }
						if (x < 0) x = -x;

						if (ans1 < ans2) ans1 += p1;
						else ans2 += p2;
					}
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

ll m;
ll can (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	map <ll, bool> old;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (old[h]) return -1;
		old[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {
		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 || p2 < 1 || p1 == p2);// ans = -1;
			else {
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }
				//for (ll x = 1; ; x++) {
				//	//ans1 + x * p1 = ans2 + y * p2;
				//	ll a = ans1 + x * p1 - ans2;
				//	ll y = a / p2;
				//	if (y * p2 == a) {
				//		ans = ans1 + x * p1;
				//		break;
				//	}
				//	if (ans1 + x * p1 > ans2 + y * p2) break;
				//}
				ll kmm = p1 * p2 / gcd (p1, p2);
				ll mxa1  = ans1 + kmm, mxa2 = ans2 + kmm;

				ll cnter = 0;
				const ll one = 1;
				map <ll, bool> old;
				while (ans1 != ans2) {
					cnter ++ ;
					ll x = ans1 - ans2;
					if (cnter > 20111000 )//|| old[x] || ans1 > mxa1 || ans2 > mxa2)
					{ ans1 = -1; break; }
					old[x] = true;
					if (x < 0) x = -x;
					if (ans1 < ans2) ans1 += max (one, x / p1) * p1;
					else ans2 += max (one, x / p2) * p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

bool oldx[1000001];
ll m;
ll can (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	For (i, 1000001) oldx[i] = false;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (oldx[h]) return -1;
		oldx[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {
		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 && p2 < 1) ans = -1;
			else {
				if (p1 < 1) p1 = 0;
				if (p2 < 1) p2 = 0;

				int cnter = 0;
				while (ans1 != ans2) {
					cnter ++ ;
					ll x = ans1 - ans2;
					if (cnter > 2022000 )//|| old[x])
					{ ans1 = -1; break; }
					if (x < 0) x = -x;

					if (ans1 < ans2) ans1 += p1;
					else ans2 += p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

bool oldx[1000111];
ll m;
ll can (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	For (i, 1000111) oldx[i] = false;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (oldx[h]) return -1;
		oldx[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {
		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 || p2 < 1 || p1 == p2);// ans = -1;
			else {
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }
				//for (ll x = 1; ; x++) {
				//	//ans1 + x * p1 = ans2 + y * p2;
				//	ll a = ans1 + x * p1 - ans2;
				//	ll y = a / p2;
				//	if (y * p2 == a) {
				//		ans = ans1 + x * p1;
				//		break;
				//	}
				//	if (ans1 + x * p1 > ans2 + y * p2) break;
				//}
				ll kmm = p1 * p2 / gcd (p1, p2);
				ll mxa1  = ans1 + kmm, mxa2 = ans2 + kmm;

				ll cnter = 0;
				const ll one = 1;
				map <ll, bool> old;
				while (ans1 != ans2) {
					cnter ++ ;
					ll x = ans1 - ans2;
					if (cnter > 3333000 || old[x] || ans1 > mxa1 || ans2 > mxa2) { ans1 = -1; break; }
					old[x] = true;
					if (x < 0) x = -x;
					if (ans1 < ans2) ans1 += max (one, x / p1) * p1;
					else ans2 += max (one, x / p2) * p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

ll m;
ll can (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	map <ll, bool> old;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (old[h]) return -1;
		old[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {
		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 || p2 < 1 || p1 == p2);// ans = -1;
			else {
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }
				//for (ll x = 1; ; x++) {
				//	//ans1 + x * p1 = ans2 + y * p2;
				//	ll a = ans1 + x * p1 - ans2;
				//	ll y = a / p2;
				//	if (y * p2 == a) {
				//		ans = ans1 + x * p1;
				//		break;
				//	}
				//	if (ans1 + x * p1 > ans2 + y * p2) break;
				//}
				ll kmm = p1 * p2 / gcd (p1, p2);
				ll mxa1  = ans1 + kmm, mxa2 = ans2 + kmm;

				ll cnter = 0;
				const ll one = 1;
				map <ll, bool> old;
				while (ans1 != ans2) {
					cnter ++ ;
					ll x = ans1 - ans2;
					if (cnter > 30111000 || old[x] || ans1 > mxa1 || ans2 > mxa2) { ans1 = -1; break; }
					old[x] = true;
					if (x < 0) x = -x;
					if (ans1 < ans2) ans1 += max (one, x / p1) * p1;
					else ans2 += max (one, x / p2) * p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
int gcd(int a, int b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

ll m;
ll can (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	map <ll, bool> old;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (old[h]) return -1;
		old[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1;
	ll ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {
		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 || p2 < 1 || p1 == p2);// ans = -1;
			else {
				if (ans2 < ans1) { swap (ans1, ans2); swap (p1, p2); }
				if (p1 > p2) {
					//ll x = ans2 - ans1;
					//x /= (p1 - p2);
					////ans1 + x * p1 = ans2 + x * p2;
					////x * (p1 - p2) = ans2 - ans1;
					//if (//x > 0 &&
					//	/*x * (p1 - p2) == ans2 - ans1) {
					//	ans = ans1 + x * p1;*/
					//}
					for (ll x = 1; ; x++) {
						//ans1 + x * p1 = ans2 + y * p2;
						ll a = ans1 + x * p1 - ans2;
						ll y = a / p2;
						if (y * p2 == a) {
							ans = ans1 + x * p1;
							break;
						}
						if (ans1 + x * p1 > ans2 + y * p2) break;
					}
				}
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

int oldx[1000111];
ll m;
ll can (ll h, ll a, ll x, ll y) {
	static int canCnt = 0;
	canCnt++;
	ll ans = 0;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (oldx[h] == canCnt) return -1;
		oldx[h] = canCnt;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {

		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 && p2 < 1) ans = -1;
			else {
				if (p1 < 1) p1 = 0;
				if (p2 < 1) p2 = 0;
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }

				ll cnter = 0;
				const ll one = 1;
				//map <ll, bool> old;
				while (ans1 != ans2) {
					cnter ++ ;
					ll x = ans1 - ans2;
					if (cnter > 1555000 )//|| old[x])
					{ ans1 = -1; break; }
					//old[x] = true;
					if (x < 0) x = -x;
					if (ans1 < ans2) ans1 += p1;
					else ans2 += p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
ll gcd(ll a, ll b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

bool oldx[1000111];
ll m;
ll can (ll h, ll a, ll x, ll y) {
	static int canCnt = 0;
	canCnt++;
	ll ans = 0;
	For (i, 1000111) oldx[i] = false;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (oldx[h]) return -1;
		oldx[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = -1,
		ans1 = can (h1, a1, x1, y1),
		ans2 = can (h2, a2, x2, y2);

	if (ans1 > 0 && ans2 > 0) {

		if (ans1 == ans2) ans = ans1;
		else {
			ll p1 = can (a1, a1, x1, y1),
				p2 = can (a2, a2, x2, y2);

			if (p1 < 1 && p2 < 1) ans = -1;
			else {
				if (p1 < 1) p1 = 0;
				if (p2 < 1) p2 = 0;
				if (ans1 < ans2) { swap (ans1, ans2); swap (p1, p2); }

				ll cnter = 0;
				const ll one = 1;
				//map <ll, bool> old;
				while (ans1 != ans2) {
					cnter ++ ;
					ll x = ans1 - ans2;
					if (cnter > 2000111 )//|| old[x])
					{ ans1 = -1; break; }
					//old[x] = true;
					if (x < 0) x = -x;
					if (ans1 < ans2) ans1 += p1;
					else ans2 += p2;
				}

				ans = ans1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;		//	typedef unsigned long long  ull;
typedef pair <ll, ll> pll;//	typedef pair <double, double> pdd;
typedef vector <int> VI;
#define MP make_pair
const int INF = 2147483647, MOD = 1000*1000*1000 + 7;
const double eps = 1e-8; // (eps < 1e-15) is the fact (1e-14)
#define For(i, n) for (int i = 0; i < (n); i++)
#define For1(i, n) for (int i = 1; i <= (n); i++)
//#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << "_" << err << "_"; exit(0); }
int gcd(int a, int b) { return (b==0)? a: gcd(b, a%b); }
/*-------------------------------------------------------------------------------------*/

ll m;
bool can (ll h, ll a, ll x, ll y) {
	map <ll, bool> old;
	while (h != a) {
		if (old[h]) {
			return false;
		}
		old[h] = true;
		h = x * h + y;
		h %= m;
	}
	return true;
}
ll can2 (ll h, ll a, ll x, ll y) {
	ll ans = 0;
	while (h != a) {
		ans++;
		h = x * h + y;
		h %= m;
	}
	return ans;
}
ll can3 (ll a, ll x, ll y) {
	ll ans = 0;
	ll h = a;
	map <ll, bool> old;
	do {
		ans++;
		h = x * h + y;
		h %= m;
		if (old[h]) return -1;
		old[h] = true;
	}while (h != a);
	return ans;
}
/*_____________________________________________________________________________________*/

int main() {
	ll h1, a1, x1, y1;
	ll h2, a2, x2, y2;
	cin >> m
		>> h1 >> a1 >> x1 >> y1
		>> h2 >> a2 >> x2 >> y2;

	ll ans = 0;

	if (can (h1, a1, x1, y1) && can (h2, a2, x2, y2)) {
		ll ans1 = can2 (h1, a1, x1, y1),
			ans2 = can2 (h2, a2, x2, y2);
		ll p1 = can3 (a1, x1, y1),
			p2 = can3 (a2, x2, y2);

		if (ans1 == ans2) ans = ans1;
		else {
			if (p1 == -1 || p2 == -1 || p1 == p2) ans = -1;
			else {
				if (ans1 < ans2) {
					while (ans1 != ans2) {
						ans1 += p1;
						ans2 += p2;
						if (ans1 > ans2) { ans = -1; break; }
					}
				}
				else {
					while (ans1 != ans2) {
						ans1 += p1;
						ans2 += p2;
						if (ans1 < ans2) { ans = -1; break; }
					}
				}
			}
		}
	}
	else ans = -1;

	cout << ans << endl;

	return 0;
}
/*

*/


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vpi;

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

#define pb push_back
#define rsz resize
#define sz(x) int(x.size())
#define all(x) begin(x), end(x)
#define f first
#define s second

int n,m,k;

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin>>n>>m>>k;
    int len=0;
    int x=1,y=1;
    vpi cur;
    vector<vpi> p;
    vpi l;
    bool lef=0;
    while(y!=n+1)
    {
        ++len;
        if(k>1)
        {
            cur.pb({x,y});
            if(len==2)
            {
                p.push_back(cur);
                cur.clear();
                len=0;
                --k;
            }
        }
        else l.pb({x,y});
        if(lef)--x;
        else ++x;
        if(x==0)
        {
            x=1;
            ++y;
            lef=0;
        }
        else if(x==m+1)
        {
            x=m;
            ++y;
            lef=1;
        }
    }
    trav(e,p)
    {
        cout<<2<<' '<<e[0].f<<' '<<e[0].s<<' '<<e[1].f<<' '<<e[1].s<<'\n';
    }
    if(sz(l)>0)
    {
        cout<<sz(l)<<' ';
        trav(e,l)cout<<e.f<<' '<<e.s<<' ';
        cout<<'\n';
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vpi;

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

#define pb push_back
#define rsz resize
#define sz(x) int(x.size())
#define all(x) begin(x), end(x)
#define f first
#define s second

int n,m,k;

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin>>n>>m>>k;
    int x=1,y=1;
    int cnt=0;
    int len=n*m - 2*(k-1);
    while(k>1)
    {
        for(; y<= n; ++y)
        {
            for(x=1; x+1<=m;x+=2)
            {
                if(k==1)break;
                cout<<2<<' '<<x<<' '<<y<<' '<<x+1<<' '<<y<<'\n';
                --k;
            }
            if(k==1)break;
        }
        if(k!=1&&(m&1)&&y+1<=n)
        {
            cout<<2<<' '<<n<<' '<<y<<' '<<n<<' '<<y+1<<'\n';
            --k;
        }
    }
    --x;
    bool lef=1;
    vpi p;
    while(y<=n)
    {
        if(lef)++x;
        else --x;
        if(x==m+1)
        {
            x=m;
            ++y;
            lef=1;
        }
        else if(x==-1)
        {
            x=0;
            ++y;
            lef=0;
        }
        p.pb({x,y});
    }
    if(len>0)
    {
        cout<<len<<' ';
        trav(e,p)
        {
            cout<<e.f<<' '<<e.s<<' ';
        }
        cout<<'\n';
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vpi;

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

#define pb push_back
#define rsz resize
#define sz(x) int(x.size())
#define all(x) begin(x), end(x)
#define f first
#define s second

int n,m,k;

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin>>n>>m>>k;
    int len=0;
    int x=1,y=1;
    int idex=0;
    vpi cur;
    vector<vpi> p;
    vpi l;
    bool lef=0;
    while(y!=n+1)
    {
        ++len;
        if(k>1)
        {
            cur.pb({x,y});
            if(len==2)
            {
                p.push_back(cur);
                cur.clear();
                len=0;
                --k;
            }
        }
        else l.pb({x,y});
        if(lef)--x;
        else ++x;
        if(x==0)
        {
            x=1;
            ++y;
            lef=0;
        }
        else if(x==m+1)
        {
            x=m;
            ++y;
            lef=1;
        }
    }
    trav(e,p)
    {
        cout<<2<<' '<<e[0].f<<' '<<e[1].s<<' '<<e[2].f<<' '<<e[3].s<<'\n';
    }
    if(sz(l)>0)
    {
        cout<<sz(l)<<' ';
        trav(e,l)cout<<e.f<<' '<<e.s<<' ';
        cout<<'\n';
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vpi;

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

#define pb push_back
#define rsz resize
#define sz(x) int(x.size())
#define all(x) begin(x), end(x)
#define f first
#define s second

int n,m,k;

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin>>n>>m>>k;
    int x=1,y=1;
    int cnt=0;
    int len=n*m - 2*(k-1);
    while(k>1)
    {
        for(; y<= n; ++y)
        {
            for(x=1; x+1<=m;x+=2)
            {
                if(k==1)break;
                cout<<2<<' '<<x<<' '<<y<<' '<<x+1<<' '<<y<<'\n';
                --k;
            }
            if(k==1)break;
        }
        if(k!=1&&(m&1)&&y+1<=n)
        {
            cout<<2<<' '<<n<<' '<<y<<' '<<n<<' '<<y+1<<'\n';
            --k;
        }
    }
    --x;
    bool lef=0;
    vpi p;
    while(y<=n)
    {
        if(lef)--x;
        else ++x;
        if(x==m+1)
        {
            x=m;
            ++y;
            lef=1;
        }
        else if(x==0)
        {
            x=1;
            ++y;
            lef=0;
        }
        p.pb({x,y});
    }
    if(len>0)
    {
        cout<<len<<' ';
        trav(e,p)
        {
            cout<<e.f<<' '<<e.s<<' ';
        }
        cout<<'\n';
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vpi;

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

#define pb push_back
#define rsz resize
#define sz(x) int(x.size())
#define all(x) begin(x), end(x)
#define f first
#define s second

int n,m,k;

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin>>n>>m>>k;
    int len=0;
    int x=1,y=1;
    vpi cur;
    vector<vpi> p;
    vpi l;
    bool lef=0;
    while(y!=n+1)
    {
        ++len;
        if(k>1)
        {
            cur.pb({y,x});
            if(len==2)
            {
                p.push_back(cur);
                cur.clear();
                len=0;
                --k;
            }
        }
        else l.pb({y,x});
        if(lef)--x;
        else ++x;
        if(x==0)
        {
            x=1;
            ++y;
            lef=0;
        }
        else if(x==m+1)
        {
            x=m;
            ++y;
            lef=1;
        }
    }
    trav(e,p)
    {
        cout<<2<<' '<<e[0].f<<' '<<e[0].s<<' '<<e[1].f<<' '<<e[1].s<<'\n';
    }
    if(sz(l)>0)
    {
        cout<<sz(l)<<' ';
        trav(e,l)cout<<e.f<<' '<<e.s<<' ';
        cout<<'\n';
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vpi;

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

#define pb push_back
#define rsz resize
#define sz(x) int(x.size())
#define all(x) begin(x), end(x)
#define f first
#define s second

int n,m,k;

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin>>n>>m>>k;
    int len=0;
    int y=1,x=1;
    vpi cur;
    vector<vpi> p;
    vpi l;
    bool lef=0;
    while(x!=n+1)
    {
        ++len;
        if(k>1)
        {
            cur.pb({y,x});
            if(len==2)
            {
                p.push_back(cur);
                cur.clear();
                len=0;
                --k;
            }
        }
        else l.pb({y,x});
        if(lef)--y;
        else ++y;
        if(y==0)
        {
            y=1;
            ++x;
            lef=0;
        }
        else if(y==m+1)
        {
            y=m;
            ++x;
            lef=1;
        }
    }
    trav(e,p)
    {
        cout<<2<<' '<<e[0].f<<' '<<e[0].s<<' '<<e[1].f<<' '<<e[1].s<<'\n';
    }
    if(sz(l)>0)
    {
        cout<<sz(l)<<' ';
        trav(e,l)cout<<e.f<<' '<<e.s<<' ';
        cout<<'\n';
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vpi;

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

#define pb push_back
#define rsz resize
#define sz(x) int(x.size())
#define all(x) begin(x), end(x)
#define f first
#define s second

int n,m,k;

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin>>n>>m>>k;
    int x=1,y=1;
    int len=n*m - 2*(k-1);
    while(k>1)
    {
        for(; y<= n; ++y)
        {
            for(x=1; x+1<=m;x+=2)
            {
                if(k==1)break;
                cout<<2<<' '<<x<<' '<<y<<' '<<x+1<<' '<<y<<'\n';
                --k;
            }
            if(k==1)break;
        }
        if(k!=1&&(m&1)&&y+1<=n)
        {
            cout<<2<<' '<<n<<' '<<y<<' '<<n<<' '<<y+1<<'\n';
            --k;
        }
    }
    --x;
    bool lef=0;
    vpi p;
    while(y<=n)
    {
        if(lef)--x;
        else ++x;
        if(x==m+1)
        {
            x=m;
            ++y;
            lef=1;
        }
        else if(x==0)
        {
            x=1;
            ++y;
            lef=0;
        }
        if(y>m)break;
        p.pb({x,y});
    }
    if(len>0)
    {
        cout<<len<<' ';
        trav(e,p)
        {
            cout<<e.f<<' '<<e.s<<' ';
        }
        cout<<'\n';
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vpi;

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

#define pb push_back
#define rsz resize
#define sz(x) int(x.size())
#define all(x) begin(x), end(x)
#define f first
#define s second

int n,m,k;

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin>>n>>m>>k;
    int x=1,y=1;
    int len=n*m - 2*(k-1);
    vpi p;
    bool lef=0;
    while(y<=n)
    {
        while(y<= n)
        {
            while((lef&&x-1>=1)||(!lef&&x+1<=m))
            {
                if(k>1)
                {
                    cout<<2<<' '<<x<<' '<<y<<' ';
                    if(lef)cout<<x-1;
                    else cout<<x+1;
                    cout<<' '<<y<<'\n';
                    --k;
                }
                else
                {
                    p.pb({x,y});
                    if(lef)p.pb({x-1,y});
                    else p.pb({x+1,y});
                }
                if(lef)x-=2;
                else x+=2;
            }
            if((x==1||(x==m&&(m&1)))&&y+1<=n)
            {
                if(k>1)
                {
                    cout<<2<<' '<<x<<' '<<y<<' '<<x<<' '<<y+1<<'\n';
                    --k;
                }
                else
                {
                    p.pb({x,y});
                    p.pb({x,y+1});
                }
            }
            if(lef)++x;
            else --x;
            ++y;
            lef=!lef;
        }
    }
    if(p[sz(p)-1]!=pii (m,n) )p.pb({m,n});
    cout<<len<<' ';
    trav(e,p)
    {
        cout<<e.f<<' '<<e.s<<' ';
    }
    cout<<'\n';

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n,m,k;
	cin>>n>>m>>k;
	int count=k;
	bool a[n+1][m+1];
	for(int i=1;i<=n;i++)
	{
		 for(int j=1;j<=m;j++)
		 	a[i][j]=false;
	}
	for(int j=1;j<m;j+=2)
	{
	 if(count==1)
	 	break;
	 for(int i=1;i<=n;i++)
	 {
         if(count==1)
         	break;
         count--;
         cout<<2<<" "<<i<<" "<<j<<" "<<i<<" "<<j+1<<endl;
         a[i][j]=true;
         a[i][j+1]=true;
	 }
	 if(count==1)
	 	break;
   }
   int x;
   for(int j=1;j<=m;j++)
   	if(!a[n][j])
   	{
   		 x=j;
   		 break;
   	}
   int y;
   for(int i=1;i<=n;i++)
   {
   	   if(!a[i][x])
   	   {
   	   	  y=i;
   	   	  break;
   	   }
   }
   y--;
   vector<pair<int,int> >v;
   int res;
   if(abs(n-y)&1)
   	res=0;
   else
   	res=1;
   for(int i=n;i>y;i--)
   {
   	   if(res&1)
   	   	for(int j=m;j>=x;j--)
   	   		v.emplace_back(make_pair(i,j));
   	   else
   	   for(int j=x;j<=m;j++)
   	   	 v.emplace_back(make_pair(i,j));
   	   res++;
   }
   for(int i=y;i>=1;i--)
   {
   	   if(res&1)
   	   	for(int j=m;j>=x+2;j--)
   	   		v.emplace_back(make_pair(i,j));
   	   else
   	   	for(int j=x+2;j<=m;j++)
   	   		v.emplace_back(make_pair(i,j));
   	   	res++;
   }
   cout<<v.size()<<" ";
   for(auto x:v)
   	cout<<x.first<<" "<<x.second<<" ";
   cout<<endl;
   return 0;
 }


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n,m,k;
	cin>>n>>m>>k;
	int count=k;
	bool a[n+1][m+1];
	for(int i=1;i<=n;i++)
	{
		 for(int j=1;j<=m;j++)
		 	a[i][j]=false;
	}
	for(int i=1;i<n;i+=2)
	{
	 if(count==1)
	 	break;
	 for(int j=1;j<=m;j++)
	 {
         if(count==1)
         	break;
         count--;
         cout<<2<<" "<<i<<" "<<j<<" "<<i+1<<" "<<j<<endl;
         a[i][j]=true;
         a[i+1][j]=true;
	 }
	 if(count==1)
	 	break;
   }
   int x;
   for(int j=1;j<=m;j++)
   	if(!a[n][j])
   	{
   		 x=j;
   		 break;
   	}
   int y;
   for(int i=1;i<=n;i++)
   {
   	   if(!a[i][x])
   	   {
   	   	  y=i;
   	   	  break;
   	   }
   }
   y--;
   vector<pair<int,int> >v;
   int res;
   if(abs(n-y)&1)
   	res=0;
   else
   	res=1;
   for(int i=n;i>=y;i--)
   {
   	   if(res&1)
   	   	for(int j=m;j>=x;j--)
   	   		v.emplace_back(make_pair(i,j));
   	   else
   	   for(int j=x;j<=m;j++)
   	   	 v.emplace_back(make_pair(i,j));
   	   res++;
   }
   for(int i=y;i>1;i--)
   {
   	   if(res&1)
   	   	for(int j=m;j>=x+2;j--)
   	   		v.emplace_back(make_pair(i,j));
   	   else
   	   	for(int j=x+2;j<=m;j++)
   	   		v.emplace_back(make_pair(i,j));
   	   	res++;
   }
   cout<<v.size()<<" ";
   for(auto x:v)
   	cout<<x.first<<" "<<x.second<<" ";
   cout<<endl;
   return 0;
 }


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n,m,k;
	cin>>n>>m>>k;
	int count=k;
	bool a[n+1][m+1];
	for(int i=1;i<=n;i++)
	{
		 for(int j=1;j<=m;j++)
		 	a[i][j]=false;
	}
	for(int j=1;j<m;j+=2)
	{
	 if(count==1)
	 	break;
	 for(int i=1;i<=n;i++)
	 {
         if(count==1)
         	break;
         count--;
         cout<<2<<" "<<i<<" "<<j<<" "<<i<<" "<<j+1<<endl;
         a[i][j]=true;
         a[i][j+1]=true;
	 }
	 if(count==1)
	 	break;
   }
   int x;
   for(int j=1;j<=m;j++)
   	if(!a[n][j])
   	{
   		 x=j;
   		 break;
   	}
   int y;
   for(int i=1;i<=n;i++)
   {
   	   if(!a[i][x])
   	   {
   	   	  y=i;
   	   	  break;
   	   }
   }
   y--;
   vector<pair<int,int> >v;
   int res;
   if(abs(x-y)&1)
   	res=0;
   else
   	res=1;
   for(int i=n;i>y;i--)
   {
   	   if(res&1)
   	   	for(int j=m;j>=x;j--)
   	   		v.emplace_back(make_pair(i,j));
   	   else
   	   for(int j=x;j<=m;j++)
   	   	 v.emplace_back(make_pair(i,j));
   	   res++;
   }
   for(int i=y;i>=1;i--)
   {
   	   if(res&1)
   	   	for(int j=m;j>=x+2;j--)
   	   		v.emplace_back(make_pair(i,j));
   	   else
   	   	for(int j=x+2;j<=m;j++)
   	   		v.emplace_back(make_pair(i,j));
   	   	res++;
   }
   cout<<v.size()<<" ";
   for(auto x:v)
   	cout<<x.first<<" "<<x.second<<" ";
   cout<<endl;
   return 0;
 }


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n,m,k;
	cin>>n>>m>>k;
	int count=k;
	bool a[n+1][m+1];
	for(int i=1;i<=n;i++)
	{
		 for(int j=1;j<=m;j++)
		 	a[i][j]=false;
	}
	for(int i=1;i<n;i+=2)
	{
	 if(count==1)
	 	break;
	 for(int j=1;j<=m;j++)
	 {
         if(count==1)
         	break;
         count--;
         cout<<2<<" "<<i<<" "<<j<<" "<<i+1<<" "<<j<<endl;
         a[i][j]=true;
         a[i+1][j]=true;
	 }
	 if(count==1)
	 	break;
   }
   int x;
   for(int j=1;j<=m;j++)
   	if(!a[n][j])
   	{
   		 x=j;
   		 break;
   	}
   int y;
   for(int i=1;i<=n;i++)
   {
   	   if(!a[i][x])
   	   {
   	   	  y=i;
   	   	  break;
   	   }
   }
   y--;
   vector<pair<int,int> >v;
   int res;
   if(abs(n-y)&1)
   	res=0;
   else
   	res=1;
   for(int i=n;i>y;i--)
   {
   	   if(res&1)
   	   	for(int j=m;j>=x;j--)
   	   		v.emplace_back(make_pair(i,j));
   	   else
   	   for(int j=x;j<=m;j++)
   	   	 v.emplace_back(make_pair(i,j));
   	   res++;
   }
   for(int i=y;i>=1;i--)
   {
   	   if(res&1)
   	   	for(int j=m;j>=x+2;j--)
   	   		v.emplace_back(make_pair(i,j));
   	   else
   	   	for(int j=x+2;j<=m;j++)
   	   		v.emplace_back(make_pair(i,j));
   	   	res++;
   }
   cout<<v.size()<<" ";
   for(auto x:v)
   	cout<<x.first<<" "<<x.second<<" ";
   cout<<endl;
   return 0;
 }


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n,m,k;
	cin>>n>>m>>k;
	int count=k;
	bool a[n+1][m+1];
	for(int i=1;i<=n;i++)
	{
		 for(int j=1;j<=m;j++)
		 	a[i][j]=false;
	}
	for(int j=1;j<m;j+=2)
	{
	 if(count==1)
	 	break;
	 for(int i=1;i<=n;i++)
	 {
         if(count==1)
         	break;
         count--;
         cout<<2<<" "<<i<<" "<<j<<" "<<i<<" "<<j+1<<endl;
         a[i][j]=true;
         a[i][j+1]=true;
	 }
	 if(count==1)
	 	break;
   }
   int x;
   for(int j=1;j<=m;j++)
   	if(!a[n][j])
   	{
   		 x=j;
   		 break;
   	}
   int y;
   for(int i=1;i<=n;i++)
   {
   	   if(!a[i][x])
   	   {
   	   	  y=i;
   	   	  break;
   	   }
   }
   y--;
   vector<pair<int,int> >v;
   int res=0;
   for(int i=n;i>y;i--)
   {
   	   if(res&1)
   	   	for(int j=m;j>=x;j--)
   	   		v.emplace_back(make_pair(i,j));
   	   else
   	   for(int j=x;j<=m;j++)
   	   	 v.emplace_back(make_pair(i,j));
   	   res++;
   }
   for(int i=y;i>=1;i--)
   {
   	   if(res&1)
   	   	for(int j=m;j>=x+2;j--)
   	   		v.emplace_back(make_pair(i,j));
   	   else
   	   	for(int j=x+2;j<=m;j++)
   	   		v.emplace_back(make_pair(i,j));
   	   	res++;
   }
   cout<<v.size()<<" ";
   for(auto x:v)
   	cout<<x.first<<" "<<x.second<<" ";
   cout<<endl;
   return 0;
 }


#include<bits/stdc++.h>
using namespace std;
 int a,b,c,d,n,i,j;
    vector<pair<int,int> >v;
    vector<int>ans;
int row,col;
int main()
{

  cin>>row>>col>>n;
  int px=1;
  int py=1;
  int flag=1;
  for(i=1;i<=row*col;i++)
  {
      v.push_back({px,py});
      //cout<<px<<" "<<py<<endl;
      if(px%2==1)
      {
          py++;
      }
      else if(px%2==0) py--;
     if(py==col+1)
     {
         px++;
         py=col;
     }
     if(py==0)
     {
         py=1;
         px++;
     }
  }
  int ctn=0;
  vector<vector<pair<int,int> > >vv;
  vector<pair<int,int > >temp;
  int p=(row*col)/n;

  for(i=0;i<v.size();i++)
  {
      temp.push_back(v[i]);
      if(temp.size()==p)
      {
          vv.push_back(temp);
          temp.clear();
      }

  }
  cout<<vv.size()<<endl;
  for(i=0;i<vv.size()-1;i++)
  {

      cout<<vv[i].size();
      for(j=0;j<vv[i].size();j++)
      {
          cout<<" "<<vv[i][j].first<<" "<<vv[i][j].second;
      }
      cout<<endl;
  }
  cout<<vv[vv.size()-1].size()+temp.size();
  for(j=0;j<vv[i].size();j++)
  {
       cout<<" "<<vv[vv.size()-1][j].first<<" "<<vv[vv.size()-1][j].second;

  }
  for(j=0;j<temp.size();j++)
  {
       cout<<" "<<temp[j].first<<" "<<temp[j].second;
  }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
 int a,b,c,d,n,i,j;
    vector<pair<int,int> >v;
    vector<int>ans;
int row,col;
int main()
{

  cin>>row>>col>>n;
  int px=1;
  int py=1;
  int flag=1;
  for(i=1;i<=row*col;i++)
  {
      v.push_back({px,py});
      if(px%2==1)
      {
          py++;
      }
      else if(px%2==0) py--;
     if(py==col+1)
     {
         px++;
         py=col;
     }
     if(py==0)
     {
         py=1;
         px++;
     }
  }
  int ctn=0;
  vector<vector<pair<int,int> > >vv;
  vector<pair<int,int > >temp;
  int p=(row*col)/n;

  for(i=0;i<row*col;i++)
  {
      temp.push_back(v[i]);
      if(temp.size()==p)
      {
          vv.push_back(temp);
          temp.clear();
      }

  }
  for(i=0;i<vv.size();i++)
  {
      cout<<vv[i].size();
      for(j=0;j<vv[i].size();j++)
      {
          cout<<" "<<vv[i][j].first<<" "<<vv[i][j].second;
      }
      cout<<endl;
  }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
 int a,b,c,d,n,i,j;
    vector<pair<int,int> >v;
    vector<int>ans;
int row,col;
int main()
{

  cin>>row>>col>>n;
  int px=1;
  int py=1;
  int flag=1;
  for(i=1;i<=row*col;i++)
  {
      v.push_back({px,py});
      //cout<<px<<" "<<py<<endl;
      if(px%2==1)
      {
          py++;
      }
      else if(px%2==0) py--;
     if(py==col+1)
     {
         px++;
         py=col;
     }
     if(py==0)
     {
         py=1;
         px++;
     }
  }
  int ctn=0;
  vector<vector<pair<int,int> > >vv;
  vector<pair<int,int > >temp;
  int p=(row*col)/n;

  for(i=0;i<v.size();i++)
  {
      temp.push_back(v[i]);
      if(temp.size()==p)
      {
          vv.push_back(temp);
          temp.clear();
      }

  }
 // cout<<vv.size()<<endl;
  for(i=0;i<vv.size()-1;i++)
  {

      cout<<vv[i].size();
      for(j=0;j<vv[i].size();j++)
      {
          cout<<" "<<vv[i][j].first<<" "<<vv[i][j].second;
      }
      cout<<endl;
  }
  cout<<vv[vv.size()-1].size()+temp.size();
  for(j=0;j<vv[i].size();j++)
  {
       cout<<" "<<vv[vv.size()-1][j].first<<" "<<vv[vv.size()-1][j].second;

  }
  for(j=0;j<temp.size();j++)
  {
       cout<<" "<<temp[j].first<<" "<<temp[j].second;
  }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
 int a,b,c,d,n,i,j;
    vector<pair<int,int> >v;
    vector<int>ans;
int row,col;
int main()
{

  cin>>row>>col>>n;
  int px=1;
  int py=1;
  int flag=1;
  for(i=1;i<=row*col;i++)
  {
      v.push_back({px,py});
      //cout<<px<<" "<<py<<endl;
      if(px%2==1)
      {
          py++;
      }
      else if(px%2==0) py--;
     if(py==col+1)
     {
         px++;
         py=col;
     }
     if(py==0)
     {
         py=1;
         px++;
     }
  }
  int ctn=0;
  vector<vector<pair<int,int> > >vv;
  vector<pair<int,int > >temp;
  int p=(row*col)/n;

  for(i=0;i<v.size();i++)
  {
      temp.push_back(v[i]);
      if(temp.size()==p)
      {
          vv.push_back(temp);
             temp.clear();
         if( vv.size()==n-1)
          {

              i++;
               break;

          }

      }
  }
  for(;i<v.size();i++)
  {
      temp.push_back(v[i]);

  }
 if(temp.size()!=0) vv.push_back(temp);

  for(i=0;i<vv.size();i++)
  {

      cout<<vv[i].size();
      for(j=0;j<vv[i].size();j++)
      {
          cout<<" "<<vv[i][j].first<<" "<<vv[i][j].second;
      }
      cout<<endl;
  }

}


#include<bits/stdc++.h>
using namespace std;
 int a,b,c,d,n,i,j;
    vector<pair<int,int> >v;
    vector<int>ans;
int row,col;
int main()
{

  cin>>row>>col>>n;
  int px=1;
  int py=1;
  int flag=1;
  for(i=1;i<=row*col;i++)
  {
      v.push_back({px,py});
      //cout<<px<<" "<<py<<endl;
      if(px%2==1)
      {
          py++;
      }
      else if(px%2==0) py--;
     if(py==col+1)
     {
         px++;
         py=col;
     }
     if(py==0)
     {
         py=1;
         px++;
     }
  }
  int ctn=0;
  vector<vector<pair<int,int> > >vv;
  vector<pair<int,int > >temp;
  int p=(row*col)/n;

  for(i=0;i<v.size();i++)
  {
      temp.push_back(v[i]);
      if(temp.size()==p)
      {
          vv.push_back(temp);
             temp.clear();
         if( vv.size()==n-1)
          {

              i++;
               break;

          }

      }
  }
  for(;i<v.size();i++)
  {
      temp.push_back(v[i]);

  }
  vv.push_back(temp);

  for(i=0;i<vv.size();i++)
  {

      cout<<vv[i].size();
      for(j=0;j<vv[i].size();j++)
      {
          cout<<" "<<vv[i][j].first<<" "<<vv[i][j].second;
      }
      cout<<endl;
  }

}


#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
// #define mp         make_pair
#define MAX 200005
#define CHAR_SIZE 26
#define N 301
using namespace std;

ll vis[N][N];
ll n,m,k;

void dfs(ll i,ll j,ll len)
{
	vis[i][j]=1;

	cout<<i+1<<" "<<j+1<<" ";

	if(len==1)
		return;

	if(j<m-1 && vis[i][j+1]==0)
		dfs(i,j+1,len-1);

	else if(j>0 && vis[i][j-1]==0)
		dfs(i,j-1,len-1);

	else if(i<n-1 && vis[i+1][j]==0)
		dfs(i+1,j,len-1);

}

void solve(int cno)
{
	ll i,j,l;
	cin>>n>>m>>k;

	ll d=(n*m)/k;

	ll mn=d;
	ll mx=d+((n*m)%k);

	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			if(vis[i][j]==0)
			{
				if(mn>0)
				{
					cout<<mn<<" ";
					dfs(i,j,mn);
					cout<<endl;
				}
				else
				{
					if(mx>0)
					{
						cout<<mx<<" ";
						dfs(i,j,mx);
						cout<<endl;
					}
				}
			}
		}
	}

}
// cout<<mn<<endl;
/*

*/

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int TESTS=1;
	// cin>>TESTS;
	ll  i=1;
	while(TESTS--)
	{
		solve(i);
		i++;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
// #define mp         make_pair
#define MAX 200005
#define CHAR_SIZE 26
#define N 301
using namespace std;

ll vis[N][N];
ll n,m,k;

void dfs(ll i,ll j,ll len)
{
	vis[i][j]=1;

	cout<<i+1<<" "<<j+1<<" ";

	if(len==1)
		return;

	if(j<m-1 && vis[i][j+1]==0)
		dfs(i,j+1,len-1);

	else if(j>0 && vis[i][j-1]==0)
		dfs(i,j-1,len-1);

	else if(i<n-1 && vis[i+1][j]==0)
		dfs(i+1,j,len-1);

}

void solve(int cno)
{
	ll i,j,l;
	cin>>n>>m>>k;

	ll d=(n*m)/k;

	ll mn=d;
	ll mx=d+((n*m)%k);

	// cout<<mn<<" "<<mx<<endl;

	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			vis[i][j]=0;
		}
	}
	ll cnt=1;
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			if(vis[i][j]==0)
			{
				if(cnt<k)
				{
					cnt++;
					cout<<mn<<" ";
					dfs(i,j,mn);
					cout<<endl;
				}
				else if(cnt==k)
				{
					cnt++;
					cout<<mx<<" ";
					dfs(i,j,mx);
					cout<<endl;
				}
			}
		}
	}

	// cout<<mn<<" "<<mx<<endl;

}
// cout<<mn<<endl;
/*

*/

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int TESTS=1;
	// cin>>TESTS;
	ll  i=1;
	while(TESTS--)
	{
		solve(i);
		i++;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
// #define mp         make_pair
#define MAX 200005
#define CHAR_SIZE 26
#define N 301
using namespace std;

ll vis[N][N];
ll n,m,k;

void dfs(ll i,ll j,ll len)
{
	vis[i][j]=1;

	cout<<i+1<<" "<<j+1<<" ";

	if(len==1)
		return;

	if(j<m-1 && vis[i][j+1]==0)
		dfs(i,j+1,len-1);

	else if(j>0 && vis[i][j-1]==0)
		dfs(i,j-1,len-1);

	else if(i<n-1 && vis[i+1][j]==0)
		dfs(i+1,j,len-1);

}

void solve(int cno)
{
	ll i,j,l;
	cin>>n>>m>>k;

	ll d=(n*m)/k;

	ll mn=d;
	ll mx=d+((n*m)%k);

	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			if(vis[i][j]==0)
			{
				if(k>1)
				{
					k--;
					cout<<mn<<" ";
					dfs(i,j,mn);
					cout<<endl;
				}
				else
				{

						cout<<mx<<" ";
						dfs(i,j,mx);
						cout<<endl;

				}
			}
		}
	}

}
// cout<<mn<<endl;
/*

*/

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int TESTS=1;
	// cin>>TESTS;
	ll  i=1;
	while(TESTS--)
	{
		solve(i);
		i++;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
// #define mp         make_pair
#define MAX 200005
#define CHAR_SIZE 26
#define N 301
using namespace std;

ll vis[N][N];
ll n,m,k,mx,mn=2;

void dfs(ll i,ll j,ll len)
{
	if((len==2&&k>1) || len==mx)
		cout<<len<<" ";
	vis[i][j]=1;

	cout<<i+1<<" "<<j+1<<" ";

	if(len==1)
	{
		k--;
		cout<<endl;
		if(k==1)
			len=mx+1;
		else
			len=mn+1;
	}

	if(j<m-1 && vis[i][j+1]==0)
		dfs(i,j+1,len-1);

	else if(j>0 && vis[i][j-1]==0)
		dfs(i,j-1,len-1);

	else if(i<n-1 && vis[i+1][j]==0)
		dfs(i+1,j,len-1);

}

void solve(int cno)
{
	ll i,j,l;
	cin>>n>>m>>k;

	ll d=2*(k-1);
	mx=(n*m)-d;

	// cout<<mn<<" "<<mx<<endl;
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			vis[i][j]=0;
		}
	}

	if(k==1)
	{
		dfs(0,0,n*m);
	}
	else
	{
		dfs(0,0,2);
	}

	// cout<<mn<<" "<<mx<<endl;

}
// cout<<mn<<endl;
/*

*/

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int TESTS=1;
	// cin>>TESTS;
	ll  i=1;
	while(TESTS--)
	{
		solve(i);
		i++;
	}
	return 0;
}