

//================code===================//

//#define TLE

#ifdef TLE
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#endif

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <ctime>

#define ci(t) cin>>t
#define co(t) cout<<t
#define LL long long
#define ld long double
#define fa(i,a,b) for(LL i=(a);i<(LL)(b);++i)
#define fd(i,a,b) for(LL i=(a);i>(LL)(b);--i)
#define setp pair<pair<int,int>,int>
#define setl pair<LL,LL>

#define M_PI 3.14159265358979323846
#define micro 0.000001

using namespace std;

#ifdef OHSOLUTION
#define ce(t) cerr<<t
#define AT cerr << "\n=================ANS=================\n"
#define AE cerr << "\n=====================================\n"
LL gcd(LL a, LL b) { return a % b ? gcd(b, a % b) : b; }
LL lcm(LL a, LL b) { return (a * b) / gcd(a, b); }
#else
#define AT
#define AE
#define ce(t)
#define __popcnt __builtin_popcount
#define gcd __gcd
#define lcm __lcm
#endif


pair <int, int> vu[9] = { {0,1},{1,0},{0,-1} ,{-1,0},{0,0},{1,1}, {-1,1} , {1,-1},{-1,-1} }; //RDLU EWSN

template<typename T, typename U> void ckmax(T& a, U b) { a = a < b ? b : a; }
template<typename T, typename U> void ckmin(T& a, U b) { a = a > b ? b : a; }
struct gcmp { bool operator()(LL a, LL b) { return a < b; } bool operator()(setl a, setl b) { return a.second < b.second; } };
struct lcmp { bool operator()(LL a, LL b) { return a > b; } bool operator()(setl a, setl b) { return a.second > b.second; } };

const int max_v = 2e5 + 7;
const int INF = 1e9 + 7;
const LL LNF = (LL)1e18 + 7ll;
const LL mod = 1e9 + 7;

int v, e;
vector <setl> adj[max_v];
LL dist[max_v][57];
bool vist[max_v][57];

struct node
{
	LL first, second;
	LL cost;
};

struct cmp
{
	bool operator()(const node& x, const node& b)
	{
		return x.second > b.second;
	}
};

int main()
{
#ifdef OHSOLUTION
	freopen("input.txt", "r", stdin);
#endif	
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	ci(v >> e);

	fa(i, 0, e)
	{
		int x, y, cost; ci(x >> y >> cost), --x, --y;
		adj[x].push_back({ y,cost });
		adj[y].push_back({ x,cost });
	}	

	priority_queue<node, vector<node>, cmp> pq;
	fa(i, 0, v) fa(j, 0, 51) dist[i][j] = INF;
	dist[0][0] = 0;
	pq.push({ 0,0 ,0});

	while (!pq.empty())
	{
		node cur;
		do
		{
			cur = pq.top(); pq.pop();
		} while (!pq.empty() && vist[cur.first][cur.cost]);

		if (vist[cur.first][cur.cost]) break;
		vist[cur.first][cur.cost] = 1;

		if (cur.cost)
		{
			for (auto x : adj[cur.first])
			{
				if (dist[x.first][0] > dist[cur.first][cur.cost] + (cur.cost + x.second) * (cur.cost + x.second))
				{
					dist[x.first][0] = dist[cur.first][cur.cost] + (cur.cost + x.second) * (cur.cost + x.second);
					pq.push({ x.first,dist[x.first][0],0 });
				}
			}
		}
		else
		{
			for (auto x : adj[cur.first])
			{
				if (dist[x.first][x.second] > dist[cur.first][cur.cost])
				{
					dist[x.first][x.second] = dist[cur.first][cur.cost];
					pq.push({ x.first,dist[x.first][x.second],x.second });
				}
			}
		}
	}

	fa(i, 0, v) dist[i][0] == INF ? co(-1 << " ") : co(dist[i][0] << " ");


	return 0;
}