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

// KACTL start
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
// KACTL end

#ifndef DEBUG_393939
static const int __39 = [](){ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 39;}();
#endif

// read ints (+ and - both supported)
#define scan(x) do{while((x=getchar())<'0')__=x;for(x-='0';'0'<=(_=getchar());x=(x<<3)+(x<<1)+_-'0');if(__=='-')x=-x;__='\0';}while(0)
char _,__;

const int INF = 0x3f3f3f3f;

const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
struct chash { // large odd number for C
    const uint64_t C = ll(4e18 * acos(0)) | 71;
    ll operator()(ll x) const { return __builtin_bswap64((x ^ RANDOM) * C); }
    ll operator()(pii x) const { return operator()((ll(x.first) << 32) | x.second); }
};

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<pii>> graph(n * 51);
    // v * 51 + 0: original vertex
    // v * 51 + 1..50: new vertex
    auto add_directed = [&](int u, int v, int w) {
        graph[u * 51].emplace_back(v * 51 + w, 0);
        for (int j = 1; j <= 50; j++) {
            int t = j + w;
            graph[u * 51 + j].emplace_back(v * 51, t * t);
        }
    };
    rep(i, 0, m) {
        int u, v, w;
        cin >> u >> v >> w;
        u--;
        v--;
        add_directed(u, v, w);
        add_directed(v, u, w);
    }
    // min queue: (distance, vertex)
    priority_queue<pii, vector<pii>, greater<pii>> pq;
    pq.emplace(0, 0);
    vector<int> dist(n * 51, INF);
    dist[0] = 0;
    while (!pq.empty()) {
        auto [d, u] = pq.top(); pq.pop();
        if (d > dist[u]) continue;
        // dist[u] = d;
        for (auto [v, w] : graph[u]) {
            if (d + w < dist[v]) {
                dist[v] = d + w;
                pq.emplace(d + w, v);
            }
        }
    }
    cout << dist[0];
    for (int i = 1; i < n; i++) {
        int v = i * 51;
        cout << " ";
        if (dist[v] < INF) cout << dist[v];
        else cout << "-1";
    }
    cout << "\n";
    return 0;
}
