#include //#include #define INF 1e9 #define rep(i,n)for(int i=0;(i)<(int)(n);i++) #define REP(i,a,b)for(int i=(int)(a);(i)<=(int)(b);i++) #define ALL(a) (a).begin(),(a).end() #define chmax(a, b) a = max(a, b) #define chmin(a, b) a = min(a, b) #define pb push_back #define fi first #define se second #define sz(x) ((int)x.size()) using namespace std; //using namespace atcoder; using ld = long double; using ll = long long; using P = pair; using Graph = vector>; const ll ZER = 0; const ll MOD = 1e9 + 7; ll dp[501][501][2]; int main(){ // std::cin.tie(0); // std::ios::sync_with_stdio(false); int n, m; cin >> n >> m; vector> fld(n + 1, vector (n + 1, 0)); rep(i, n + 1)rep(j, n + 1)rep(k, 2)dp[i][j][k] = (INF * INF); dp[0][0][0] = 0; dp[1][1][0] = 0; dp[1][1][1] = 0; rep(i, m){ ll h, w, c; cin >> h >> w >> c; fld[h][w] = c + 1; } REP(i, 1, n){ REP(j, 1, n){ chmin(dp[i][j][0], dp[i][j - 1][0] + fld[i][j]); chmin(dp[i][j][0], dp[i - 1][j][0] + fld[i][j]); chmin(dp[i][j][1], dp[i][j - 1][0]); chmin(dp[i][j][1], dp[i - 1][j][0]); chmin(dp[i][j][1], dp[i][j - 1][1] + fld[i][j]); chmin(dp[i][j][1], dp[i - 1][j][1] + fld[i][j]); } } ll res = min(dp[n][n][0], dp[n][n][1]) + 1; cout << res << endl; // rep(i, n + 1){rep(j, n + 1)cout << dp[i][j][0] << " ";cout << endl;} // cout << endl; // rep(i, n + 1){rep(j, n + 1)cout << dp[i][j][1] << " ";cout << endl;} // cout << endl; // rep(i, n + 1){rep(j, n + 1)cout << fld[i][j] << " ";cout << endl;} // cout << endl; }