#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <algorithm>
#include <cmath>
#include <climits>
#include <iomanip>
#include <queue>
#include <stack>
#include <ctype.h>

using namespace std;

typedef long long ll;

const int INF = (1<<30)-1;
const ll LINF = (1LL<<60)-1;

#define rep(i, n) for (int i = 0; i < n; i++)

template<class T> 
bool chmax(T &a, T b) {if (a < b) {a = b;return true;}else return false;}

template<class T> 
bool chmin(T &a, T b) {if (a > b) {a = b;return true;}else return false;}

typedef pair<int,int> P;
typedef pair<ll,P> lP;

struct edge {
    int to;
    ll cost;
    edge(int to, ll cost): to(to), cost(cost) {};
};

void dijkstra(vector<vector<edge> >&G, int s, vector<vector<ll> > &dist) {
    dist.resize(G.size());
    for (int i = 0; i < G.size(); i++)
        dist[i].resize(51, LINF);
    
    priority_queue<lP, vector<lP>, greater<lP> > que;
    que.push(lP(0, P(s, 0)));
    dist[s][0] = 0;
    while (que.size()) {
        lP p = que.top();    que.pop();
        int u = p.second.first;   ll d = p.first, w = p.second.second;
        if (dist[u][w] < d)    continue;
        if (w == 0) {
            for (auto e: G[u]) {
                int v = e.to;   ll c = e.cost;
                if (chmin(dist[v][c], d))
                    que.push(lP(dist[v][c], P(v, c)));
            }
        }
        else {
            for (auto e: G[u]) {
                int v = e.to;   ll c = e.cost;
                if (chmin(dist[v][0], d + (w+c)*(w+c)))
                    que.push(lP(dist[v][0], P(v, 0)));
            }
        }
    }
}

int main() {    
    int n, m;   cin >> n >> m;
    vector<vector<edge> > G(n);
    rep(i, m) {
        int u, v;   ll w;   cin >> u >> v >> w;
        u--;    v--;
        G[u].push_back(edge(v, w));
        G[v].push_back(edge(u, w));
    }
    vector<vector<ll> > dist;
    dijkstra(G, 0, dist);
    rep(i, n) {
        if (dist[i][0] < LINF)
            cout << dist[i][0] << " ";
        else 
            cout << -1 << " ";
    }
    cout << endl;
    return 0;
}

//小数点精度
//cout << fixed << std::setprecision(15) << y << endl;