#include<bits/stdc++.h>
using namespace std;
#define ll           long long
#define rep(i,j,n)   for(ll i=j;i<n;i++)
#define scn(a)       scanf("%lld",&a)
#define scns(a,b)    scanf("%lld %lld",&a,&b)
#define print(a)     printf("%d\n",a)
#define vec          vector<ll>
#define pb           push_back    
#define pairs        pair<ll,ll>
#define f            first
#define s            second
#define all(v)       v.begin(),v.end()
#define mem(a,b)     memset(a,b,sizeof(a))
#define tc           ll t; cin>>(t); while(t--)
const int N = 2e5+5;
vector<pairs> v[N];
ll n,m;
vector<vector<ll>> dist(N,vector<ll>(55,INT_MAX));
void dijsktra()
{
	typedef tuple<ll,ll,ll> tpl;
	priority_queue<tpl,vector<tpl>,greater<tpl>> pq;
	pq.push({0,1,0});

	while(!pq.empty())
	{
		tpl val=pq.top();
		ll u=get<1>(val),wgt=get<2>(val);
		pq.pop();

		for(auto t:v[u])
		{
			ll curWt=t.s,prevWt=wgt,cur_dist=0;

			if(prevWt)
			{
				curWt=0;
				cur_dist=(prevWt+t.s)*(prevWt+t.s);
			}

			if(dist[t.f][curWt]>dist[u][prevWt]+cur_dist)
			{
				dist[t.f][curWt] = dist[u][prevWt]+cur_dist;
				pq.push({dist[t.f][curWt],t.f,curWt});
			}
		}
	}
}
int main()
{
    scns(n,m);

	rep(i,0,m)
	{
		ll x,y,wt;
		scns(x,y); scn(wt);
		v[x].pb({y,wt});
		v[y].pb({x,wt});
	}

	dist[1][0]=0;
	dijsktra();

	rep(i,1,n+1)
	{
		if(dist[i][0]==INT_MAX)
		cout<<-1<<" ";
		else cout<<dist[i][0]<<" ";
	}
}