#include using namespace std; const long long INF = 1000000000000000000; int main(){ int N, M; cin >> N >> M; vector> A(N, vector(N, 0)); for (int i = 0; i < M; i++){ int h, w, c; cin >> h >> w >> c; h--; w--; A[h][w] = c; } vector>> dp(N, vector>(N, vector(2, INF))); dp[0][0][0] = 0; for (int i = 0; i < N; i++){ for (int j = 0; j < N; j++){ if (i < N - 1){ dp[i + 1][j][0] = min(dp[i + 1][j][0], dp[i][j][0] + A[i + 1][j] + 1); dp[i + 1][j][1] = min(dp[i + 1][j][1], dp[i][j][1] + A[i + 1][j] + 1); dp[i + 1][j][1] = min(dp[i + 1][j][1], dp[i][j][0] + 1); } if (j < N - 1){ dp[i][j + 1][0] = min(dp[i][j + 1][0], dp[i][j][0] + A[i][j + 1] + 1); dp[i][j + 1][1] = min(dp[i][j + 1][1], dp[i][j][1] + A[i][j + 1] + 1); dp[i][j + 1][1] = min(dp[i][j + 1][1], dp[i][j][0] + 1); } } } cout << dp[N - 1][N - 1][1] << endl; }