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

#define int long long
#define double long double

const double PI = 3.14159265358979323846;
const int INF = 9223372036854775807/2; //\!!!!!!!!!!!!!!!!!!!!!!
const int mod = INF;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

int fastPow(int b, int e){int r = 1;while(e){if(e%2 == 1){r*=b;r%=mod;}b*=b;b%=mod;e/=2;}return r;}
int gcd(int a, int b){ if(a%b == 0) return b; else return gcd(b, a%b);}
int sign(int a){if(a < 0){ return -1;}if(a == 0) {return 0;}return 1;}
bool isPrime(int a){if(a <= 1) {return false;}int f = a;for(int i = 2; i*i<=f; i++){if(a%i == 0){return false;}}return true;}
int toInt(string s){int tot = 0;for(int i = s.size()-1; i >= 0; i--){tot+=((s[i]-'0')%mod)*fastPow(10,i);tot%=mod;}return tot;}
string toString(int a){string s = "";while(a){s = (char)('0'+a%10) + s;a/=10;}return s;}

signed main(){
   	ios::sync_with_stdio(false);
	cin.tie(0);

	int n;
	cin >> n;

	int m;
	cin >> m;

	vector< vector< pair<int,int> > > Graph((n+1));

	for(int i = 0; i < m; i++){
		int a, b, c;
		cin >> a >> b >> c;

		Graph[a].push_back({b,c});
		Graph[b].push_back({a,c});
	}

	priority_queue< pair<int,int>, vector< pair<int,int> >, greater< pair<int,int> > > pq;

	vector< int > dist((n+4)*51, INF);

	dist[51] = 0;
	pq.push({0,51});

	while(!pq.empty()){
		pair<int,int> p = pq.top();
		int node = (p.second/51);
		int cost = p.first;
        int last = p.second%51;

		pq.pop();

		if(dist[p.second] != cost) continue;

		for(auto pa : Graph[node]){
			int b = pa.first*51;
			int wa = pa.second;

            if(last == 0){
                //FIRST EDGE
                b += wa;
                if(dist[b] > cost){
                    dist[b] = cost;
                    pq.push({cost,b});
                }
            }else{
                //SECOND EDGE

                if(dist[b] > cost + (wa+last)*(wa+last)){
                    dist[b] = cost+(wa+last)*(wa+last);
                    pq.push({dist[b],b});
                }
            }
		}
	}

	for(int i = 1; i <= n; i++){
		if(dist[i*51] == INF) dist[51*i] = -1;
		cout << dist[i*51] << ' ';
	}
	cout << endl;
}
