// 解き直し.
// https://atcoder.jp/contests/abc191/editorial/610
// C++(GCC 9.2.1)
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define repex(i, a, b, c) for(int i = a; i < b; i += c)
#define repx(i, a, b) repex(i, a, b, 1)
#define rep(i, n) repx(i, 0, n)
#define repr(i, a, b) for(int i = a; i >= b; i--)
#define pb push_back
const LL INF = 202020202020202020;

struct edge{
    int n; // 次の頂点.
    LL c;  // 移動時間.
    bool operator < (const edge &E) const{ return c > E.c; }
};

LL d[2020][2020]; // i -> j の 移動時間.
LL ds[2020];      // i -> i の 移動時間.
LL ans[2020];     // 出力用.
vector<edge> G[2020];

int main(){
    
    // 1. 入力情報.
    int N, M;
    scanf("%d %d", &N, &M);
    rep(i, N) rep(j, N) d[i][j] = INF;
    rep(i, N) ds[i] = INF;
    rep(i, M){
        int a, b;
        LL c;
        scanf("%d %d %lld", &a, &b, &c);
        a--, b--;
        edge e;
        e.n = b;
        e.c = c;
        G[a].pb(e);                       // a -> b.
        if(a == b) ds[a] = min(ds[a], c); // a -> a.  (random_10.txt の WA回避？)
    }
    
    // 2. ダイクストラ法.
    // https://ja.wikipedia.org/wiki/ダイクストラ法
    auto dijkstra = [&](int s){
        // 1. 初期化.
        priority_queue<edge> pq;
        
        // 2. 始点設定.
        d[s][s] = 0;
        pq.push({s, 0LL});
        
        // 3. キュー pq が 空になるまで, 探索.
        while(!pq.empty()){
            // 4. 探索を継続するために, 頂点を取得.
            edge e = pq.top();
            pq.pop();
            int cn = e.n;
            LL cc = e.c;
            
            // 4. 各隣接する頂点について, 距離を確認し, 移動時間を更新.
            for(auto &g : G[cn]){
                int nn = g.n;
                LL nc = cc + g.c;
                
                // 5. 移動時間更新.
                if(d[s][nn] > nc) d[s][nn] = nc, pq.push({nn, nc});
            }
        }
        return;
    };
    
    // 3. 各頂点に対して, 最短経路を求める.
    rep(i, N) dijkstra(i);
    
    // 4. 出力用の情報を作成.
    rep(i, N){
        ans[i] = ds[i];
        rep(j, N) if(i != j) ans[i] = min(ans[i], d[i][j] + d[j][i]);
    }
    rep(i, N) if(ans[i] >= INF) ans[i] = -1;
    
    // 5. 出力.
    rep(i, N) printf("%lld\n", ans[i]);
    return 0;
    
}
#include<bits/stdc++.h>
using namespace std;


#define endline         '\n'
#define vll             vector<ll>
#define vi              vector<int>
#define pll             pair<ll,ll>
#define pi              pair<int,int>
#define pb              push_back
#define fr(i,a,b)       for(int i=a;i<(int)b;i++)
#define rfr(i,a,b)      for(int i=a;i>=(int)b;i--)
#define ff              first
#define ss              second
#define all(x)          (x).begin(), (x).end()
#define INF             0x3f3f3f3f
#define debug(x)        cerr<<x<<endline
#define PI              3.14159265358979323846

typedef long long int ll;
const int N = 4e5+10;
const ll mod=1e9+7;


clock_t startTime;
double getCurrenttime() {
    return (double) (clock() - startTime) / CLOCKS_PER_SEC;
}
bool comp(pi a,pi b)
{
    if(a.first==b.first)return a.second<b.second;
    return a.first<b.first;
}

void solve()
{
    vector<pair<ll,pll>> v;
    int n;cin>>n;
    fr(i,0,n)
    {
        ll x,y,z; cin>>x>>y>>z;
        v.pb({x,{y,z}});
    }
    int c=0;
    sort(all(v));

    fr(i,0,n)
    {
        fr(j,i+1,n)
        {
            double a[]={0,0,0.00001,0.00001};
            double b[]={0,-0.00001,0,-0.00001};
            int f1=v[i].first;int f2=v[j].first;
            long double x1=0,y1=0,x2=0,y2=0;
            fr(k,0,4)
            {
                if(f1==k+1)
                {
                    x1=v[i].second.first+a[k],y1=v[i].second.second+b[k];
                }
            }
            fr(k,0,4)
            {
                if(f2==k+1)
                {
                    x2=v[j].second.first+a[k],y2=v[j].second.second+b[k];
                }
            }
            if(x1<=x2 and x2<=y1)c++;
            else if(x2<=x1 and x1<=y2)c++;
            else if(x1<=y2 and y2<=y1)c++;
            else if(x2<=y1 and y1<=y2)c++;
        }
    }
    cout<<c<<endline;
}

int main()
{
    std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL);
    
    #ifndef ONLINE_JUDGE
    startTime=clock();
    freopen("input.txt","r",stdin);
    freopen("output1.txt","w",stdout);
    // freopen("debug.txt", "w", stderr);
    #endif

    int t=1;
    // cin>>t;
    while(t--)
    {
        solve();
    }

    #ifndef ONLINE_JUDGE
    cout << "Time: " << getCurrenttime() * 1000 << " ms" << endline;
    #endif    
}