//#pragma GCC optimize("ofast")
//#pragma GCC optimize("o3")
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define fr(i,x,y) for(int i=x;i<y;i++)
#define rfr(i,x,y) for(int i=x;i>=y;i--)
using namespace std;
const int N=2e5,M=51,INF=2e18,MOD1=1e9+9,MOD2=1e9+7,L=20,P=11,Q=3e7;
int d[N][M];
bool used[N][M];
vector<pair<int,int> > gr[N];
void solve()
{
    int n,m;
    cin>>n>>m;
    fr(i,0,m)
    {
        int a,b,c;
        cin>>a>>b>>c;
        gr[a].push_back({b,c});
        gr[b].push_back({a,c});
    }
    fr(i,0,n+1)fr(j,0,M)d[i][j]=INF;
    set<pair<int,pair<int,int> > > och;
    d[1][0]=0;
    och.insert({0,{1,0}});
    while(och.size()>0)
    {
        auto j=*och.begin();
        och.erase(och.begin());
        if(used[j.second.first][j.second.second])continue;
        used[j.second.first][j.second.second]=1;
        for(auto z:gr[j.second.first])
        {
            int to=z.first;
            int c=0;
            int dd=j.first;
            if(j.second.second!=0)
            {
                c=0;
                dd+=(j.second.second+z.second)*(j.second.second+z.second);
            }
            else c=z.second;
            if(dd<d[to][c])
            {
                d[to][c]=dd;
                och.insert({dd,{to,c}});
            }
        }
    }
    fr(i,1,n+1)
    {
        if(d[i][0]==INF)cout<<-1<<' ';
        else cout<<d[i][0]<<' ';
    }
}
signed main()
{
//freopen("021.dat","r",stdin);
//freopen("encryption.out","w",stdout);
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t=1;
    //cin>>t;
    while(t--)solve();
}