using namespace std;
#include <bits/stdc++.h>
int n, m;
vector<pair<int, int>> adj[100011];
int visited[100001][51];
vector<vector<int>> dist(100001, vector<int>(51, 1e9));
struct cmp
{
  bool operator()(pair<int, int> a, pair<int, int> b)
  {
    return a.first > b.first;
  }
};
void simple_dijkstra()
{
  priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> q;
  q.push({0ll, 1 * 55 + 0});
  dist[1][0] = 0ll;
  // we try add like this (created value........distance)
  // .............distance.......created value.........(as you want)
  // .....created value ======= last weight= cv/55.......... last node=cv%55
  while (!q.empty())
  {
    int currnode = q.top().second / 55;
    int lastweight = q.top().second % 55;
    int currdis = q.top().first;
    // debug() << imie(q.top());
    q.pop();
    if (visited[currnode][lastweight])
      continue;
    // if (dist[currnode][lastweight] < currdis)
    //   continue;
    visited[currnode][lastweight] = 1;
    dist[currnode][lastweight] = currdis;
    for (auto it : adj[currnode])
    {
      int newlastweight = it.second;
      int newdis = currdis;
      if (lastweight != 0)
      {
        newdis += (lastweight + it.second) * (it.second + lastweight);
      }
      if (lastweight)
        newlastweight = 0;
      if (visited[it.first][newlastweight])
        continue;
      q.push({newdis, 55 * it.first + newlastweight});
    }
  }
}
void solve()
{
  cin >> n >> m;
  for (int i = 0; i < m; i++)
  {
    int a, b, c;
    cin >> a >> b >> c;
    adj[a].push_back({b, c});
    adj[b].push_back({a, c});
  }
  simple_dijkstra();
  for (int i = 1; i <= n; i++)
  {
    if (dist[i][0] == 1e9)
      dist[i][0] = -1;
    cout << dist[i][0] << " ";
  }
  cout << endl;
}
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // #ifndef ONLINE_JUDGE
  //   freopen("input.txt", "r", stdin);
  //   freopen("output.txt", "w", stdout);
  // #endif
  int t = 1;
  //cin >> t;
  while (t--)
    solve();
  return 0;
}
