#include<bits/stdc++.h>

using namespace std;

#define fr(i, n) for(int i = 0; i < n; i++)
#define frr(i, n) for(int i = 1; i <= n; i++)
#define frm(i, n) for(int i = n-1; i >= 0; i--)

#define pb push_back
#define f first
#define s second

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> ponto;
typedef vector<vector<ll>> matrix;

#define mem(v, k) memset(v, k, sizeof(v));

#define db cout << "Debug" << endl;

#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

#define mp make_pair
#define pq priority_queue

#define mx(a, b) a = max(a, b);
#define mod(a, b) a = a%b;

#define MAXN 100010
#define MOD 1000000007
#define MAXL 30
#define ROOT 1
#define INF 1000000000000000100

int dist[MAXN][55], vis[MAXN][55];
vector<pii> adj[MAXN];
int n, m;

template <class T>
using pqt = priority_queue<T,vector<T>,greater<T>>;
void dijk(){
	pqt<pair<ll,ll>> pq;
	pq.emplace(0,0);
	while(!pq.empty()){
		ll dac, noo; tie(dac,noo) = pq.top(); pq.pop();
		ll no = noo/51;
		ll id = noo%51;
		if(vis[no][id]) continue;
		vis[no][id] = 1;
		dist[no][id] = dac;
	
		if(id==0){
			for(auto &[it,c] : adj[no]){
				if(vis[it][c] or dac >= dist[it][c]) continue;
				dist[it][c] = dac;
				pq.emplace(dac,51*it+c);
			}	
		} else{
			for(auto &[it,c] : adj[no]){
				int aux = (id + c)*(id + c);
				if(vis[it][0] or dac + aux >= dist[it][0]) continue;
				dist[it][0] = dac+aux;
				pq.emplace(dac+aux,51*it);
			}	
		}
	}
}

int main(){
	fastio;

	cin >> n >> m;
	fr(i, m){
		int u, v, w;
		cin >> u >> v >> w;
		adj[u-1].pb({v-1, w});
		adj[v-1].pb({u-1, w});
	}
	fr(i, n) fr(j, 55) dist[i][j] = MOD;

	dijk();

	fr(i, n){
		if(!vis[i][0]) cout << -1 << " ";
		else cout << dist[i][0] << " ";
	}
	cout << endl;
}