#include<bits/stdc++.h>
#define fr first
#define sc second
using namespace std;
typedef long long ll;
template<class T>bool umin(T &a,T b){if(b<a){a=b;return true;}return false;}
ll dp[100005][55];
vector<pair<int,int>>g[100005];
int n,m;
ll inf=9e9;
ll sq(ll x){
    return x*x;
}
int main(){
    memset(dp,0x3f,sizeof dp);
    scanf("%d%d",&n,&m);
    while(m--){
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        g[x].push_back({y,z});
        g[y].push_back({x,z});
    }
    typedef pair<int,pair<int,short int>> data_type;
    priority_queue<data_type,vector<data_type>,greater<data_type>>q;
    q.push({0,{1,0}});
    dp[1][0]=0;
    while(!q.empty()){
        auto tmp=q.top();
        int v=tmp.sc.fr;
        int flag=tmp.sc.sc;
        q.pop();
        if(!flag){
            for(auto to:g[v])
                if(umin(dp[to.fr][to.sc],dp[v][0]))
                    q.push({dp[to.fr][to.sc],{to.fr,to.sc}});
        }else{
            for(auto to:g[v])
                if(umin(dp[to.fr][0],dp[v][flag]+sq(flag+to.sc)))
                    q.push({dp[to.fr][0],{to.fr,0}});
        }
    }
    for(int i=1;i<=n;i++){
        if(dp[i][0]>int(2e9))dp[i][0]=-1;
        printf("%lld ",dp[i][0]);
    }
}
