#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("Ofast")
using namespace std;

#define int int64_t
#define SQ(a) (a)*(a)

const int INF = 1e15;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, m;
  cin >> n >> m;

  vector<vector<pair<int, int>>> g(n);
  for (int i = 0, a, b, c; i < m; i++) {
    cin >> a >> b >> c;
    a--, b--;
    g[a].push_back({b, c});
    g[b].push_back({a, c});
  }

  vector<int> deven(n, INF);
  vector<vector<int>> dodd(51, vector<int> (n, INF));

  deven[0] = 0;
  priority_queue<array<int, 3>> Q; // {dist, v, (-1 OR w_last)}
  Q.push({0, 0, -1});

  vector<vector<bool>> vis(52, vector<bool> (n));
  while (!Q.empty()) {
    int d = -Q.top()[0];
    int v =  Q.top()[1];
    int lw=  Q.top()[2];
    Q.pop();

    if (vis[lw + 1][v])
      continue;
    vis[lw + 1][v] = true;

    if (lw == -1) { // even steps
      for (auto [u, w] : g[v]) {
        if (dodd[w][u] > d) {
          dodd[w][u] = d;
          Q.push({-d, u, w});
        }
      }
    } else {
      for (auto [u, w] : g[v]) {
        if (deven[u] > d + SQ(lw + w)) {
          deven[u] = d + SQ(lw + w);
          Q.push({-deven[u], u, -1});
        }
      }
    }
  }

  for (int i = 0; i < n; i++)
    cout << (deven[i] == INF ? -1 : deven[i]) << " ";
  cout << "\n";
}