#include<bits/stdc++.h>
#define LL long long 
#define fi first
#define se second
#define mp make_pair
#define PLL pair<LL, LL>
#define PIL pair<int, LL>
#define PII pair<int, int>

using namespace std;

const int N = (int)1e5 + 7;
const int inf = 0x3f3f3f3f;

int n, m;
int d[N][51][2];
int ans[N];

vector<PII> G[N];

void solve() {
	scanf("%d%d", &n, &m);
	memset(d, inf, sizeof(d));
	for (int i = 1; i <= m; i++) {
		int u, v, w;
		scanf("%d%d%d", &u, &v, &w);
		G[u].push_back({w, v});
		G[v].push_back({w, u});
	}
	priority_queue<pair<PII, PII>, vector<pair<PII, PII>>, greater<pair<PII, PII>>> Q;
	d[1][0][0] = 0;
	Q.push({{0, 0}, {1, 0}});
	while(!Q.empty()) {
		int distance = Q.top().fi.fi;
		int flag = Q.top().fi.se;
		int u = Q.top().se.fi;
		int pw = Q.top().se.se;
		Q.pop();
		if (distance > d[u][pw][flag]) continue;
		for (auto &[w, v] : G[u]) {
			int nw = flag ? distance + w * w + 2 * pw * w : distance + w * w;
			if (nw < d[v][w][flag ^ 1]) {
				d[v][w][flag ^ 1] = nw;
				Q.push({{nw, flag ^ 1}, {v, w}});
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		ans[i] = inf;
		for (int j = 0; j <= 50; j++) ans[i] = min(ans[i], d[i][j][0]);
		if (ans[i] == inf) ans[i] = -1;
	}
	for (int i = 1; i <= n ;i++) printf("%d%c", ans[i], " \n"[i == n]);
}

int main() {
	int NT = 1;
	while(NT--) solve();
	return 0;
}