#include<iostream>
#include<cstring>
#include<vector>
#include<utility>
#include<queue>
#define endl "\n"
#define pb push_back
#define fi first
#define se second

using namespace std;

const int N = 2e5 + 10;

typedef pair<int,int> PII;

int n,m;
vector<PII>v[N];

int dist[N],flag[N],dao[N];
void dij(){
	memset(dao,0x3f,sizeof dao);
	memset(dist,0x3f,sizeof dist);
	memset(flag,0,sizeof flag);
	priority_queue<PII,vector<PII>,greater<PII> > q;
	q.push({0,1});
	dist[1] = 0;
	while(q.size()){
		PII p = q.top();
		q.pop();
		if(flag[p.se]) continue;
		flag[p.se] = 1;
		for(int i = 0; i < v[p.se].size(); i++){
			int j = v[p.se][i].fi;
			int k = v[p.se][i].se;
			if(dao[j] > k){
				for(int l = 0; l < v[j].size(); l++){
					int g = v[j][l].fi;
					int gg = v[j][l].se;
					if(dist[g] > dist[p.se] + (k + gg) * (k + gg)){
						dist[g] = dist[p.se] + (k + gg) * (k + gg);
						q.push({dist[g],g});
					}
				}
				dao[j] = k;
			}
		}
	}
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i = 1; i <= m; i++){
		int x,y,z;
		scanf("%d%d%d",&x,&y,&z);
		v[x].pb({y,z});
		v[y].pb({x,z});
	}
	
	dij();
	
	for(int i = 1; i <= n; i++){
		if(dist[i] != 0x3f3f3f3f)
		printf("%d ",dist[i]);
		else printf("-1 ");
	}
	
	
	
	return 0;
}
