//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 100000000000000
#define MAX_V 101


int V;
LL d[MAX_V][MAX_V];

void wf(){
	REP(k,V)REP(i,V)REP(j,V){
		d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
	}
}

int main(){
	int E;
	cin>>V>>E;
	REP(i,V)REP(j,V)d[i][j]=(i==j)?0:INF;
	REP(i,E){
		int a,b,c;
		cin>>a>>b>>c;
		d[a][b]=c;
	}
	wf();
	REP(i,V)if(d[i][i]<0){
		cout<<"NEGATIVE CYCLE"<<endl;
		return 0;
	}
	REP(i,V){
		REP(j,V){
			if(d[i][j]>2*10e9)cout<<"INF";
			else cout<<d[i][j];
			if(j!=V-1)cout<<" ";
		}
		cout<<endl;
	}
	return 0;
}
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <functional>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <utility>
#include <vector>

using namespace std;

typedef long long Long;
#define whole(xs) xs.begin(), xs.end()
#define uniq(xs) (xs.erase(unique(xs.begin(), xs.end()), xs.end()))

template<class T>
ostream& operator<<(ostream& os, const vector<T>& vs) {
    if (vs.empty()) return os << "[]";
    os << "[" << vs[0];
    for (int i = 1; i < vs.size(); i++) os << " " << vs[i];
    return os << "]";
}

const int IINF = 1<<28;
const Long LINF = 1LL<<56;
#define INF IINF

int V, E;

struct Edge {
    int s, t, w;
    Edge(int s, int t, int w) : s(s), t(t), w(w) {}
    bool operator<(const Edge& e) const {
        return w < e.w;
    }
};

vector<Edge> G;

void Input() {
    cin >> V >> E;
    for (int i = 0; i < E; i++) {
        int s, t, w; cin >> s >> t >> w;
        G.push_back(Edge(s, t, w));
    }
}

struct UnionFind {
    int N;
    vector<int> P;
    UnionFind(int N) : N(N) {
        P.clear(); P.resize(N, -1);
    }
    int query(int x) {
        if (P[x] == -1) return x;
        return P[x] = query(P[x]);
    }
    void merge(int x, int y) {
        x = query(x); y = query(y);
        if (x == y) return;
        P[y] = x;
    }
};

void Solve() {
    sort(whole(G));
    UnionFind uf(V);
    int Ans = 0;
    for (int i = 0; i < E; i++) {
        Edge& e = G[i];
        if (uf.query(e.s) == uf.query(e.t)) continue;
        Ans += e.w;
        uf.merge(e.s, e.t);
    }
    cout << Ans << endl;
}

int main() {
    Input(); Solve();
    return 0;
}