# include <bits/stdc++.h>
using namespace std;

#define ll          long long
#define int          ll
#define pb          push_back
#define ppb         pop_back
#define eb          emplace_back
#define endl		'\n'
#define max(a,b)	((a)>(b)?(a):(b))
#define min(a,b)	((a)<(b)?(a):(b))
#define vi          vector<int>
#define vii         vector<pair<int, int> >
#define mp          make_pair
#define mem1(a)     memset((a),-1,sizeof(a))
#define mem0(a)     memset((a),0,sizeof(a))
#define memf(a)     memset((a),false,sizeof(a))
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    (a)*((b)/gcd((a),(b)))
#define DECIMAL(n)  cout << fixed ; cout << setprecision(n);
#define FAST        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mi          map<int,int>
#define sz(x)       (ll)(x).size()
#define rep(i,a,b)  for(int i=(a);i<(b);i++)
#define pii         pair<int,int>
#define INF         1000000000000000000

vector<pii> adj[100001];
int ans[100001];
int d[100001];
int p[100001];

int solve()
{
	int n;
	cin >> n;
	int m;
	cin >> m;
	rep(i, 0, m)
	{
		int u, v, w;
		cin >> u >> v >> w;
		adj[u].pb({v, w});
		adj[v].pb({u, w});
	}
	rep(i, 1, n + 1)
	{
		sort(all(adj[i]));
	}
	ans[1] = 0;
	d[1] = INF;
	rep(i, 2, n + 1)
	{
		ans[i] = INF;
		d[i] = INF;
		p[i] = -1;
	}
	set<pair<int, int>> q;
	// set<int> abc;
	q.insert({0, 1});
	bool visited[100001];
	memf(visited);
	while (!q.empty())
	{
		vi abc;
		auto it = q.begin();
		// set<int> abc;
		int w = q.begin()->F;
		int v = q.begin()->S;
		// cout << v << endl;
		// int w1=it.S.S;
		visited[v] = true;
		q.erase(it);
		for (auto u : adj[v])
		{
			if (d[u.F] > u.S)
			{
				d[u.F] = u.S;
				p[u.F] = v;
				abc.pb(u.F);
			}
		}
		for (auto u : abc)
		{
			for (auto w : adj[u])
			{
				int node = w.F;
				int weight = w.S;
				if (!visited[node])
				{
					if (ans[node] > (ans[p[u]] + (d[u] + weight) * (d[u] + weight)))
					{
						q.erase({ans[node], node});
						ans[node] = (ans[p[u]] + (d[u] + weight) * (d[u] + weight));
						q.insert({ans[node], node});
					}
				}
			}
		}

	}
	rep(i, 1, n + 1)
	{
		if (ans[i] < INF)
			cout << ans[i] << " ";
		else
			cout << "-1 ";

	}

	return 0;
}
signed main()
{
	FAST
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int t = 1;
	// cin>>t;
	while (t)
	{
		solve();
		t--;
	}
	return 0;
}