#include<iostream>
#include<utility>
using namespace std;
typedef long long ll;
#define fi(i,a,b) for(int i = a; i <= b; ++i)
#define fr(i,a,b) for(int i = a; i >= b; --i)
#define x first
#define y second
#define sz(x) ((int)(x).size())
#define pb push_back
using pii = pair<int,int>;
int a[2005][2005];
int f[2005][2005];
//#define DEBUG
int minn = 0x3f3f3f3f;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,m;
cin >> n >> m;
fi(j,1,m) fi(i,1,n) cin >> a[j][i];
fi(i,1,n) fi(j,1,m){
f[i][j] = min(j == 1 ? f[i-1][m]:f[i-1][j-1],f[i-1][j]) + a[j][i];
// cout << f[i][j] << " ";
}
fi(i,1,m) minn = min(f[n][i],minn);
// fi(i,1,n) fi(j,1,m) cout << f[i][j] << " ";
cout << minn << endl;
#ifdef DEBUG
//freopen(D:\in.txt,r,stdin);
#endif
return 0;
}