#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<int> vi;

const int N = 1e5;
const int64_t mod = 1LL + N;

template<typename U, typename V>
U getMax(U u, V v){
    if (u > v) return u;
    return (U)v;
};

vector<vector<pair<int,int>>>E(N, vector<pair<int,int>>());
vector<int64_t> dist(N, 1e9);
vector<bool> vis(N, false);
vector<int> lastVal(N, 101);

void solve(){
   int n, m; cin >> n >> m;
   for (int i = 0; i < m; i++){
        int u,v,w; cin >> u >> v >> w;
        u--; v--;
        E[u].pb({v,w});
        E[v].pb({u,w});
   }
   auto dijkstra = [&](int src){
       dist[src] = 0LL;
       set<pair<int64_t,int>>st;
       st.insert({0LL,src});
       while (st.size()){
            auto top = *st.begin(); st.erase(st.begin());
            int v = top.second;
            if (vis[v]) continue;
            vis[v] = true;
            for (auto e1 : E[v]){
                int mid = e1.first;
                if (lastVal[mid]<=e1.second) continue;
                lastVal[mid] = e1.second;
                for (auto e2 : E[mid]){
                    if (!vis[e2.first]){
                        int64_t dd = top.first + 1LL * (e1.second + e2.second) * (e1.second + e2.second);
                        if (dd < dist[e2.first]){
                            dist[e2.first] = dd;
                            st.insert({dd, e2.first});
                        }
                    }
                }
            }
       }
   };
   dijkstra(0);
   for (int i = 0;i < n; i++){
    cout << (dist[i] == 1e9 ? -1 : dist[i]) << " ";
   }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
	ll t = 1;
	while(t--){
		solve();
	}
	return 0;
}
