#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
#define pii pair<int, int>
vector<pair<int, int> > G[maxn];
int dis[maxn][51][2];
int n, m;

void _add(int u, int v, int w) {
    G[u].push_back({v, w});
    G[v].push_back({u, w});
}

struct node {
    int id, pre, w, state;
    bool operator<(const node& p) const{
        return w > p.w;
    }
};

void dij() {
    priority_queue<node> pq;
    memset(dis, 0x3f, sizeof(dis));
    pq.push({1, 0, dis[1][0][0] = 0, 0});
    while (!pq.empty()) {
        node now = pq.top();
        pq.pop();
        int x = now.id;
        for (auto i : G[now.id]) {
            int v = i.first, w = i.second;
            int cost = 0;
            if (now.state == 1) {
                cost = (now.pre + w) * (now.pre + w);
            }
            if (dis[v][w][now.state^1] > dis[now.id][now.pre][now.state] + cost) {
                dis[v][w][now.state^1] = dis[now.id][now.pre][now.state] + cost;
                pq.push({v, w, dis[v][w][now.state^1], now.state^1});
            }
        }
    }
}

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; ++ i) {
        int u, v; int w;
        scanf("%d%d%d", &u, &v, &w);
        _add(u, v, w);
    }
    dij();
    for (int i = 1; i <= n; ++ i) {
        int ans = 0x3f3f3f3f;
        for (int j = 0; j <= 50; ++ j) {
            ans = min(dis[i][j][0], ans);
        }
        if (i != 1) printf(" ");
        if (ans == 0x3f3f3f3f) printf("-1");
        else printf("%d", ans);
    }
    puts("");
    return 0;
}
