#include<bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define f(i,n) for(int i=0;i<n;i++)
#define fr(i,a,b) for(int i=a;i<b;i++)
using namespace std;
void dijkstra(ll dist[],set<pair<ll,ll> > st,vector<pair<ll,ll> > adj[],int n){
while(!st.empty()){
auto it = st.begin();
pair<ll,ll> p = (*it);
st.erase(it);
ll dis = p.F;
ll x = p.S;
// cout<<x<<"--\n";
f(j,adj[x].size()){
int ver = adj[x][j].F;
int dis2 = adj[x][j].S;
if(dist[ver]>dis+dis2){
// cout<<"yo yo \n";
it = st.find({dist[ver],ver});
st.erase(it);
dist[ver]=dis+dis2;
st.insert({dis+dis2,ver});
}
}
}
}
ll distant[2002][2002];
ll graph[2002][2002];
void test(){
int n,m;
cin>>n>>m;
vector<pair<ll,ll> > adj[n+1];
memset(graph,-1,sizeof(graph));
memset(distant,-1,sizeof(distant));
f(i,m){
ll u,v,w;
cin>>u>>v>>w;
if(graph[u][v]!=-1){
if(graph[u][v]>w){
graph[u][v] = w;
}
}else
graph[u][v] = w;
}
f(i,n+1){
f(j,n+1){
if(i!=j){
if(graph[i][j]!=-1){
adj[i].pb({j,graph[i][j]});
}
}
}
}
ll max_dis = 1000000000000 ;
for(int i=1;i<=n;i++){
ll dist[n+1];
f(j,n+1)dist[j]=max_dis;
dist[i] = 0;
set<pair<ll,ll> > st;
fr(j,1,n+1){
st.insert({dist[j],j});
}
dijkstra(dist,st,adj,n);
for(int j=1;j<=n;j++){
if(j!=i and dist[j]!=max_dis){
distant[i][j] = dist[j];
}
}
if(graph[i][i]!=-1)distant[i][i] = graph[i][i];
}
vector<ll> sol;
for(int i=1;i<=n;i++){
ll res = max_dis*2LL;
for(int j=1;j<=n;j++){
if(distant[i][j]!=-1 and distant[j][i]!=-1){
if(i==j)res = min(res,distant[i][j]);
else res = min(res, distant[i][j] + distant[j][i]);
}
}
if(res==(max_dis*2LL))
res = -1;
sol.pb(res);
}
for(auto p : sol)cout<<p<<"\n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