// Solution explained -> https://codeforces.com/blog/entry/88012

#include <bits/stdc++.h>
using namespace std;
 
#define int long long
 
signed main(){
	cin.tie(nullptr)->sync_with_stdio(false);
	int n,m;cin>>n>>m;
	vector<vector<pair<int,int>>> adj(n);
	while(m--){
		int u,v,w;cin>>u>>v>>w;
		u--;v--;
		adj[u].emplace_back(v,w);
		adj[v].emplace_back(u,w);
	}
	vector<vector<int>> dist(n,vector<int>(51,LLONG_MAX));
	set<tuple<int,int,int>> q;
	q.emplace(0,0,0);
	dist[0][0]=0;
	auto relax=[&](int v,int a,int k){
		if(dist[v][a]>k){
			dist[v][a]=k;
			q.emplace(k,v,a);
		}
	};
	while(!q.empty()){
		auto [d,u,a]=*q.begin();
		q.erase(q.begin());
		for(auto [v,b]:adj[u]){
			if(a==0)relax(v,b,d+b*b);
			else relax(v,0,d+2*a*b+b*b);
		}
	}
	for(int i=0;i<n;i++){
		cout<<(dist[i][0]==LLONG_MAX?-1:dist[i][0])<<" \n"[i==n-1];
	}
}