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

#define LL long long
int n, x[20], y[20], z[20]; LL dp[(1<<17)+1][20];

int dis(int i, int j) {return abs(x[i]-x[j]) + abs(y[i]-y[j]) + max(0, z[j]-z[i]);}

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    
    cin >> n;
    for(int i = 0; i <= (1<<17); i++) for(auto &j : dp[i]) j = 1e9;
    for(int i = 0; i < n; i++) cin >> x[i] >> y[i] >> z[i];   
    dp[0][0] = 0;

    for(int i = 1; i < (1 << n); i++) 
        for(int j = 0; j < n; j++) 
            if((i >> j) & 1)
                for(int k = 0; k < n; k++) 
                    dp[i][j] = min(dp[i][j], dp[i - (1<<j)][k] + dis(k, j));
    cout << dp[(1<<n)-1][0];  
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(),(x).end()

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}
 
template<class T> inline bool chmax(T &a, T b){
    if(a<b){
        a = b;
        return true;
    }
    return false;
}

template<class T> inline bool chmin(T &a, T b){
    if(a>b){
        a = b;
        return true;
    }
    return false;
}
ll dp[19][1<<18];
vector<pair<int,int> > q[20];
int main(){
    int n,m;
    cin >> n >> m;

    rep(i,m){
        int x,y,z;
        cin >> x >> y >> z;
        x--;y--;
        q[x].push_back(MP(y,z));
    }
    dp[0][0] = 1;
    rep(i,n){
        rep(j,1<<n){
            rep(k,n){
                if((j>>k)&1){

                }else{
                    dp[i+1][j^(1<<k)] += dp[i][j];
                }
            }
        }
        for(auto x:q[i]){
            int mask = (1<<(x.first+1)) - 1;
            rep(j,1<<n){
                if(__builtin_popcount(mask & j) > x.second){
                    dp[i+1][j] = 0;
                }
            }
        }
    }
    cout << dp[n][(1<<n)-1] << endl;
    return 0;
}