/*
 #include <ext/pb_ds/assoc_container.hpp>
 #include <ext/pb_ds/priority_queue.hpp>

 using namespace __gnu_pbds;


__gnu_pbds::priority_queue<int, less<int>> pq;
gp_hash_table<int, int> mp;
*/

/** Begin fast allocation */
/*
const int mx_MEM = 10 * 1e7;
int mpos = 0;
char mem[mx_MEM];

inline void *operator new(size_t n) {
    assert((mpos += n) <= mx_MEM);
    return (void *) (mem + mpos - n);
}

inline void operator delete(void *) noexcept {} // must have!
*/
/** End fast allocation */

/** Pragmas */
// mt19937 gen(2281337);

//#pragma GCC optimize("O3", "Ofast", "unroll-loops", "unswitch-loops", "fast-math", "no-stack-protector")

//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,abm,mmx,avx")
// sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,abm,mmx,
//#pragma comment(linker, "/STACK:66777216")

/*
 * @author kamil.sharipov
 */

#include <bits/stdc++.h>

//#define int long long
#define all(x) (x).begin(), (x).end()
#define fo(i, a, b) for (int i = a; i < b; ++i)
#define ll long long
#define pii pair<int, int>

using namespace std;

int n, m, inf = 2e9, dst = 51;

vector<vector<pii > > adj;

vector<vector<vector<int> > > we;

int dist[100001][51];

signed main() {
#ifdef _LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    cin >> n >> m;

    adj.resize(n), we.resize(n);
    for (int i = 0; i < n; ++i) {
        we[i].resize(dst);
    }

    for (int i = 0; i < m; ++i) {
        int u, v, w;
        cin >> u >> v >> w;
        --u, --v;
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
    }

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < dst; ++j) {
            dist[i][j] = inf;
        }
    }

    dist[0][0] = 0;

    set<pair<int, pii > > st;
    st.insert({0, {0, 0}});

    while (!st.empty()) {
        auto[w, p] = *st.begin();
        st.erase(st.begin());
        int v = p.first;
        int d = p.second;

        if (w > dist[v][d]) continue;

        if (d == 0) {
            for (int i = 0; i < adj[v].size(); ++i) {
                int to = adj[v][i].first;
                int wei = adj[v][i].second;

                if (dist[to][wei] > w) {
                    dist[to][wei] = w;
                    st.insert({dist[to][wei], {to, wei}});
                }
            }
        } else {
            for (int i = 0; i < adj[v].size(); ++i) {
                int to = adj[v][i].first;
                int wei = adj[v][i].second;

                int len = (d + wei) * (d + wei) + w;

                if (dist[to][0] > len) {
                    dist[to][0] = len;
                    st.insert({dist[to][0], {to, 0}});
                }
            }
        }
    }

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




