#include<bits/stdc++.h>
using namespace std;
#define _rep(i, x, y) for(int i = (int)x; i < (int)y; ++i)
#define _dep(i,x,y) for(int i = (int)x; i > (int)y; i--)
#define PII pair<int,int>
#define eb emplace_back
#define pb push_back
#define fi first
#define se second
#define PQ priority_queue
#define lb lower_bound
#define ub upper_bound
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VII;
typedef vector<ll> VL;
typedef vector<vector<ll>> VLL;
constexpr int mod = 1e9 + 7;
constexpr ll KINF = 1e14;
constexpr double eps = 1e-7;
int main(){
	ios::sync_with_stdio(false); cin.tie(0);
	int n, m;
	cin >> n >> m;
	vector<vector<PII>> g(n);
	_rep(i, 0, m){
		int u, v, w;
		cin >> u >> v >> w;
		g[--u].eb(--v, w);
		g[v].eb(u, w);
	}
	int M = 51;
	PQ<pair<ll,int>> pq;
	VL d(M * n, KINF);
	d[0] = 0;
	pq.emplace(0, 0);
	while(!pq.empty()){
		auto [dist, t] = pq.top();
		pq.pop();
		dist = -dist;
		if(dist > d[t]) continue;
		int u = t / M, w = t % M;
		if(!w){//上次的点不是中转点
			for(auto& [vv, ww] : g[u]){
				int nxt = vv * M + ww;
				if(d[nxt] > d[t]){
					d[nxt] = d[t];
					pq.emplace(-d[nxt], nxt);
				}
			}
		}
		else{
			for(auto& [vv, ww] : g[u]){
				int nxt = vv * M;
				if(d[nxt] > d[t] + 1ll * (ww + w) * (ww + w)){
					d[nxt] = d[t] + 1ll * (ww + w) * (ww + w);
					pq.emplace(-d[nxt], nxt);
				}
			}
		}
	}
	_rep(i, 0, n) cout << (d[i * M] == KINF ? -1 : d[i * M]) << " \n"[i == n - 1];
	return 0;
} 
// 小心左移运算符导致的int溢出
// dp记得处理非法状态
// resize并不会初始化数组
// 全局函数一定要记得inline
// 带空格字符串不能直接cin,要用getline

