
// Problem : E. Paired Payment
// Contest : Codeforces - Codeforces Round #703 (Div. 2)
// URL : https://codeforces.com/contest/1486/problem/E
// Memory Limit : 512 MB
// Time Limit : 4000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ll          long long
#define pb          push_back
#define mp 	        make_pair
#define pii         pair<ll,ll>
#define vi          vector<ll>
#define vii         vector<pii>
#define mi          map<ll,ll>
#define mii         map<pii,ll>
#define all(a)      (a).begin(),(a).end()
#define fi          first
#define si          second
#define sz(x)       (ll)x.size()
#define endl        '\n'
#define mod         (ll)1000000007
#define mset(m,v) memset(m,v,sizeof(m))
using namespace std;
ll power(ll a , ll b,ll modi)
{
    a%=modi;
    ll res = 1 ;
    while(b)
    {
        if(b%2) {
            res = (res * a) % modi ;
        }
        b/=2 ;
        a = (a*a) % modi ;
    }
    return res ;
}
ll n,m;
vector<pair<ll,ll> > adj[100001];
ll deven[100001];
void dijkstra(){
	set<pair<pair<ll,ll>, pair<ll,ll> > > s;
    for(ll i=1;i<=n;i++)
    deven[i]=(ll)1e18;
	s.insert({{0,0},{0,1}});
	//distance,pre_dis,parity,cur_vertex
	deven[1]=0;
	while(!s.empty()){
		pair<pair<ll,ll>, pair<ll,ll> > p =*s.begin();
		s.erase(s.begin());
		ll d=p.first.first;
		ll par=p.second.first;
		ll pre=p.first.second;
		ll cur=p.second.second;
		if(!par && d>deven[cur])
		continue;
		for(ll i=0;i<(ll)adj[cur].size();i++){
			ll v=adj[cur][i].first;
			ll w=adj[cur][i].second;
			if(par){
				ll dis=d+(pre+w)*(pre+w);
				if(dis<deven[v]){
					s.insert({{dis,0},{0,v}});
					deven[v]=dis;
				}
			}
			else{
				s.insert({{d,w},{1,v}});
			}
		}
	}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t=1;
	//cin>>t;
	while(t--)
	{
	cin>>n>>m;
	for(ll i=0;i<m;i++){
		ll u,v,w;
		cin>>u>>v>>w;
		adj[u].pb({v,w});
		adj[v].pb({u,w});
	}
	dijkstra();
	for(ll i=1;i<=n;i++){
		if(deven[i]==(ll)1e18)
		cout<<-1<<" ";
		else
		cout<<deven[i]<<" ";
	}
	}
return 0;
}
