#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;

void testCase() {
    
}

vector<pair<int,int>> graph[1000000];
int validist[1000000];
ll dist[1000001];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    /*
    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        testCase();
    }
    */
    int n,m;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int v,u,w;
        cin >> v >> u >> w;
        graph[v].push_back({u,w});
        graph[u].push_back({v,w});
    }
    for (int i = 0; i <= n; i++) validist[i] = 1e9;
    
    for (int j = 1; j <= n; j++) dist[j] = 1e18;
    dist[1] = 0;
    priority_queue<pair<ll,int>> pq;
    pq.push({0,1});
    while (!pq.empty()) {
        auto p = pq.top();
        pq.pop();
        ll eta = -p.first;
        int node = p.second;
        if (eta > dist[node]) continue;
        for (auto edge2 : graph[node]) { //edge.first = solmu  edge.second = paino
            if (validist[edge2.first] <= edge2.second) continue;
            validist[edge2.first] = edge2.second;
            for (auto edge : graph[edge2.first]) {
                ll prevdist = dist[edge.first];
                ll newdist = eta + (edge.second+edge2.second)*(edge.second+edge2.second);
                if (newdist < prevdist) {
                    dist[edge.first] = newdist;
                    pq.push({-newdist,edge.first});
                }
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        if (dist[i] >= 1e18) cout << -1 << " ";
        else cout << dist[i] << " ";
    }
    cout << "\n";
}

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
