//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))
#define INF 10000000
#define MAX_V 101
struct edge{int to;LL cost;};
typedef pair<LL,int> P;//firstは最短距離,secondは頂点番号
int V;
vector<edge> G[MAX_V];
LL d[MAX_V];

void dijkstra(int s){
	priority_queue<P,vector<P>,greater<P> > que;
	fill(d,d+V,INF);
	d[s]=0;
	que.push(P(0,s));
	
	while(!que.empty()){
		P p=que.top();que.pop();//仮の最短距離が短い順に取り出す
		int v=p.second;
		if(d[v]<p.first)continue;
		REP(i,G[v].size()){
			edge e=G[v][i];
			if(d[e.to]>d[v]+e.cost){
				d[e.to]=d[v]+e.cost;
				que.push(P(d[e.to],e.to));//仮の最短距離と頂点の組を更新が行われる度に追加していく
			}
		}
	}
}

int main(){
	long n,k;
	while(cin>>n>>k){
		if(n==0&&k==0)break;
		V=n;
		REP(i,n)G[i].clear();
		REP(i,k){
			int ch;
			cin>>ch;
			if(ch){
				int ct,dt;
				LL et;
				cin>>ct>>dt>>et;
				ct--;dt--;
				edge ed={dt,et};
				G[ct].PB(ed);
				ed.to=ct;
				G[dt].PB(ed);
			}else{
				int a,b;
				cin>>a>>b;
				a--;b--;
				dijkstra(a);
				if(d[b]<INF)cout<<d[b]<<endl;
				else cout<<"-1"<<endl;
			}
		}
	}
	
	return 0;
}
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;

typedef long long ll;
const ll INF = 10000000000;

int main(){
	int v,e;
	ll d[100][100];
	scanf("%d %d",&v,&e);
	fill(d[0],d[0]+10000,INF);
	for(int i=0;i<v;i++) d[i][i] = 0;
	for(int i=0;i<e;i++){
		int s,t;
		ll dist;
		scanf("%d %d %lld",&s,&t,&dist);
		d[s][t] = dist;
	}
	for(int k=0;k<v;k++){
		for(int i=0;i<v;i++){
			for(int j=0;j<v;j++){
				if(d[i][k]!=INF&&d[k][j]!=INF) d[i][j] = min(d[i][j],d[i][k] + d[k][j]);
			}
		}
	}
	bool flag = false;
	for(int i=0;i<v;i++){
		if(d[i][i]<0) flag = true;
	}
	if(flag){
		printf("NEGATIVE CYCLE\n");
	}else{
		for(int i=0;i<v;i++){
			for(int j=0;j<v;j++){
				if(d[i][j]==INF) printf("INF");
				else printf("%lld",d[i][j]);
				if(j==v-1) printf("\n");
				else printf(" ");
			}
		}
	}
	return 0;
}