#include <bits/stdc++.h>

using namespace std;

#ifdef Adrian
#include "debug.h"
#else
#define debug(...) 9999
#endif

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef complex<int> point;
#define F first
#define S second
#define ii pair<ll,ll>

typedef pair<ll, ii> iii;

int main() {
#ifdef Adrian
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#else
    ios_base::sync_with_stdio(0); cin.tie(0);
#endif


    int n, m;
    cin>>n>>m;
    vector<vector<ii>> g(n);
    for(int i=0; i<m; i++)
    {
        int x, y, z;
        cin>>x>>y>>z;
        --x;--y;
        g[x].push_back({y, z});
        g[y].push_back({x, z});
    }

    const ll oo = 1e18;
    vector<vector<ll>> d(n,
            vector<ll>(51, oo));
    d[0][0] = 0;
    priority_queue<iii> pq;
    pq.push({0, {0, 0}});

    while(!pq.empty())
    {
        ll w = -pq.top().F;
        int c = pq.top().S.F;
        int l = pq.top().S.S;
        pq.pop();

        if(l == 0)
        {
            for(auto v:g[c])
                if(w < d[v.F][v.S])
                {
                    d[v.F][v.S] = w;
                    pq.push({-d[v.F][v.S], {v.F, v.S}});
                }
        } else
        {
            for(auto v:g[c])
            {
                ll t = l + v.S;
                t *= t;

                if(w + t < d[v.F][0])
                {
                    d[v.F][0] = w + t;
                    pq.push({-d[v.F][0], {v.F, 0}});
                }
            }
        }
    }

    for(int i=0; i<n; i++)
    {
        if(d[i][0] < oo) cout<<d[i][0];
        else cout<<-1;
        cout<<" \n"[i == n - 1];
    }



    return 0;
}

