#include "bits/stdc++.h" using namespace std; using ll=long long; using dd=double; using vll=vector< ll>; using vdd=vector< dd>; using vvll=vector< vll>; using vvdd=vector; using vvvll=vector< vvll>; using vvvvll=vector; using pll=pair; using tll=tuple; using qll=tuple; using vpll=vector< pll>; using vtll=vector< tll>; using vqll=vector< qll>; using vvpll=vector; using vvtll=vector; using vvqll=vector; constexpr ll INF = 1LL << 60; struct Fast{ Fast(){ cin.tie(0); ios::sync_with_stdio(false); cout<::max_digits10); } } fast; #define REPS(i, S, E) for (ll i = (S); i <= (E); i++) #define REP(i, N) REPS(i, 0, (N)-1) #define DEPS(i, S, E) for (ll i = (E); i >= (S); i--) #define DEP(i, N) DEPS(i, 0, (N)-1) #define rep(i, S, E) for (ll i = (S); i <= (E); i++) #define dep(i, E, S) for (ll i = (E); i >= (S); i--) #define each(e, v) for (auto&& e : v) #define ALL(v) (v).begin(), (v).end() #define RALL(v) (v).rbegin(), (v).rend() template inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; }return false; } template inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; }return false; } template inline T MaxE(vector&v,ll S,ll E){ T m=v[S]; rep(i,S,E)chmax(m,v[i]); return m; } template inline T MinE(vector&v,ll S,ll E){ T m=v[S]; rep(i,S,E)chmin(m,v[i]); return m; } template inline T MaxE(vector &v) { return MaxE(v,0,(ll)v.size()-1); } template inline T MinE(vector &v) { return MinE(v,0,(ll)v.size()-1); } template inline T Sum(vector &v,ll S,ll E){ T s=T(); rep(i,S,E)s+=v[i]; return s; } template inline T Sum(vector &v) { return Sum(v,0,v.size()-1); } template inline ll sz(T &v){ return (ll)v.size(); } inline ll CEIL(ll a,ll b){ return (a<0) ? -(-a/b) : (a+b-1)/b; } inline ll FLOOR(ll a,ll b){ return -CEIL(-a,b); } template struct mll_{ ll val; mll_(ll v = 0): val(v % MOD){ if (val < 0) val += MOD; } mll_ operator - () const { return -val; } mll_ operator + (const mll_ &b) const { return val + b.val; } mll_ operator - (const mll_ &b) const { return val - b.val; } mll_ operator * (const mll_ &b) const { return val * b.val; } mll_ operator / (const mll_ &b) const { return mll_(*this) /= b; } mll_ operator + (ll b) const { return *this + mll_(b); } mll_ operator - (ll b) const { return *this - mll_(b); } mll_ operator * (ll b) const { return *this * mll_(b); } friend mll_ operator + (ll a,const mll_ &b) { return b + a; } friend mll_ operator - (ll a,const mll_ &b) { return -b + a; } friend mll_ operator * (ll a,const mll_ &b) { return b * a; } friend mll_ operator / (ll a,const mll_ &b) { return mll_(a)/b; } mll_ &operator += (const mll_ &b) { val=(val+b.val)%MOD; return *this; } mll_ &operator -= (const mll_ &b) { val=(val+MOD-b.val)%MOD; return *this; } mll_ &operator *= (const mll_ &b) { val=(val*b.val)%MOD; return *this; } mll_ &operator /= (const mll_ &b) { ll c=b.val,d=MOD,u=1,v=0; while (d){ ll t = c / d; c -= t * d; swap(c,d); u -= t * v; swap(u,v); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } mll_ &operator += (ll b) { return *this += mll_(b); } mll_ &operator -= (ll b) { return *this -= mll_(b); } mll_ &operator *= (ll b) { return *this *= mll_(b); } mll_ &operator /= (ll b) { return *this /= mll_(b); } bool operator == (const mll_ &b) const { return val == b.val; } bool operator != (const mll_ &b) const { return val != b.val; } bool operator == (ll b) const { return *this == mll_(b); } bool operator != (ll b) const { return *this != mll_(b); } friend bool operator == (ll a,const mll_ &b) { return mll_(a) == b.val; } friend bool operator != (ll a,const mll_ &b) { return mll_(a) != b.val; } friend ostream &operator << (ostream &os,const mll_ &a) { return os << a.val; } friend istream &operator >> (istream &is,mll_ &a) { return is >> a.val; } static mll_ Combination(ll a,ll b){ chmin(b,a-b); if (b<0) return mll_(0); mll_ c = 1; rep(i,0,b-1) c *= a-i; rep(i,0,b-1) c /= i+1; return c; } }; using mll = mll_<1000000007LL>; using vmll = std::vector; using vvmll = std::vector; using vvvmll = std::vector; using vvvvmll = std::vector; #if 0 #include using namespace atcoder; #endif vvvll Dijkstra(ll n, vtll &hwc, pll s) { vvll mp(n,vll(n)); each(e,hwc){ ll h,w,c; tie(h,w,c) = e; mp[h][w]=c; } ll ddx[4]={0,1,0,-1}; ll ddy[4]={1,0,-1,0}; const ll M=2; vvvll dist(n,vvll(n,vll(M,INF))); priority_queue> Q; Q.push(qll(0, s.first,s.second,0)); dist[s.first][s.second][0] = 0; while (!Q.empty()){ ll d; ll x,y,z; tie(d, x,y,z) = Q.top(); Q.pop(); if (d > dist[x][y][z]) continue; rep(dz,0,M-1){ if (z==1 and dz==1)continue; rep(idx,0,3){ ll dx=ddx[idx]; ll dy=ddy[idx]; ll xx=x+dx; ll yy=y+dy; ll zz=z+dz; if (xx<0 or n<=xx or yy<0 or n<=yy)continue; ll cost=1; if (dz==0){ cost+=mp[xx][yy]; } if (chmin(dist[xx][yy][zz],dist[x][y][z]+cost)){ Q.push(qll(dist[xx][yy][zz],xx,yy,zz)); } } } } return move(dist); } void solve() { ll n,m; cin >> n >> m; vector> hwc(m); rep(i,0,m-1){ ll h; ll w; ll c; cin>>h>>w>>c; hwc[i]={h-1,w-1,c}; } vvvll dist=Dijkstra(n,hwc,{0,0}); ll ans=dist[n-1][n-1][1]; cout << ans << '\n'; } int main(){ #if 1 solve(); //cin2solve(); //generand(); #else ll t; cin >> t; rep(i, 0, t-1){ solve(); //cin2solve(); } #endif return 0; }