#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
using namespace std;
const int N = 1e7 + 7, M = 1e7 + 7, mod = 1e9 + 7, INF = 0x3f3f3f3f;
typedef long long ll;
typedef int itn;
typedef pair<int, int> PII;

int n, m, k, t;
int dist[M];

typedef struct
{
    itn y, w;
}edge;

vector<edge>G[M];

int pos(int x, int w)
{
    return x * 51 + w;
}

void __add(int x, int y, int w)
{
    G[x].push_back((edge){y, w});
}

void add(itn x, int y, int w)
{
    __add(pos(x, 0), pos(y, w), 0);
    for(int i = 1; i <= 50; ++ i) {
        __add(pos(x, i), pos(y, 0), (i + w) * (i + w));
    }
}

void dijkstra(int S)
{
    memset(dist, 0x3f, sizeof dist);
    dist[S] = 0;
    priority_queue<PII, vector<PII>, greater<PII> >q;
    q.push({0, S});
    while(q.size()) {
        itn x = q.top().second;
        int d = q.top().first;
        q.pop();
        if(d != dist[x]) continue;

        for(auto it : G[x]) {
            itn y = it.y, w = it.w;
            if(dist[y] > d + w) {
                dist[y] = d + w;
                q.push({dist[y], y});
            }
        }
    }
}

int main()
{
    //init();
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= m; ++ i) {
        int x, y, w;
        scanf("%d%d%d", &x, &y, &w);
        add(x, y, w);
        add(y, x, w);
    }
    dijkstra(pos(1, 0));
    for(int i = 1; i <= n; ++ i) {
        if(dist[pos(i, 0)] == INF)  dist[pos(i, 0)] = -1;
        printf("%d ", dist[pos(i, 0)]);
    }
    puts("");
    return 0;
}
