#include<iostream>

using namespace std;

const int MAXN = 1005, INF = 2e9;
int d[MAXN][MAXN];

int main()
{
	int n, m;
	cin >> n >> m;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++)
			d[i][j] = INF;
	for(int i = 0; i < n; i++)
		d[i][i] = 0;
	for(int i = 0; i < m; i++)
	{
		int x, y, w;
		cin >> x >> y >> w;
		d[x][y] = w;
	}
	for(int k = 0; k < n; k++)
		for(int i = 0; i < n; i++)
			for(int j = 0; j < n; j++)
				if(d[i][k] < INF && d[k][j] < INF)
					d[i][j] = min(1ll * d[i][j], 1ll * d[i][k] + d[k][j]);
	for(int i = 0; i < n; i++)
		if(d[i][i] < 0)
		{
			cout << "NEGATIVE CYCLE" << endl;
			return 0;
		}
	for(int i = 0; i < n; i++, cout << endl)
		for(int j = 0; j < n; j++)
		{
			if(d[i][j] == INF)
				cout << "INF";
			else
				cout << d[i][j];
			if(j < n - 1)
				cout << ' ';
		}
	return 0;
}
#include <bits/stdc++.h>
#define debug(...) fprintf(stderr, __VA_ARGS__)

using namespace std;
const int N = 605;

inline int input() {
	int x; char ch; while (!isdigit(ch = getchar()));
	for (x = ch ^ 48; isdigit(ch = getchar()); x = x * 10 + (ch ^ 48));
	return x;
}

inline void chkmin(int &x, int y) { x = x < y ? x : y; }
template <class T> T sqr(T x) { return x * x; }

int n, m, D1, D2;
int ansx[N * N], ansy[N * N], tot;
bool col[N][N];

void work(int D) {
	int tmp = 0;
	while(D % 4 == 0) D >>= 2, ++tmp;
	if (D % 4 == 3) return void();
	if (D % 4 == 1) {
		for (int x = 0; x <= n; ++x)
			for (int y = 0; y <= n; ++y)
				if (((x >> tmp) + (y >> tmp)) & 1) col[x][y] = true;
	}
	if (D % 4 == 2) {
		for (int x = 0; x <= n; ++x)
			for (int y = 0; y <= n; ++y)
				if ((x >> tmp) & 1) col[x][y] = true;
	}
}

int main() {
#ifdef local
	freopen("in", "r", stdin);
	freopen("out", "w", stdout);
//	freopen("log", "w", stderr);
#endif
	ios::sync_with_stdio(false);

	cin >> n >> D1 >> D2;
	m = n, n = n * 2 - 1;

	work(D1), work(D2);

	for (int x = 0; x <= n; ++x)
		for (int y = 0; y <= n; ++y) {
			if (!col[x][y]) ansx[++tot] = x, ansy[tot] = y;
			if (tot == m * m) goto Get;
		}
Get:;

	for (int i = 1; i <= tot; ++i)
		printf("%d %d\n", ansx[i], ansy[i]);
	return 0;
}