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

// template {{{
#define  range(i, l, r) for (int i = (int)(l); i < (int)(r); (i) += 1)
#define rrange(i, l, r) for (int i = (int)(r) - 1; i >= (int)(l); (i) -= 1)

#define  whole(f, x, ...) ([&](decltype((x)) container) { return (f)(  begin(container),  end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)

#define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl

using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;

constexpr i32 mod   = 1e9 + 7;
// constexpr i32 mod   = 998244353;

constexpr i32 inf   = 1001001001;
constexpr i64 infll = 1001001001001001001ll;

constexpr i32 dx[] = {0, -1, 1, 0, -1, 1, -1, 1}; 
constexpr i32 dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};

struct IoSetup { IoSetup(i32 x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;

template <typename T = i64> T input() { T x; cin >> x; return x; }

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != (int)v.size() ? " " : ""); } return os; } 
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }

template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }

template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
// }}}

// dijkstra {{{
template < typename CostType >
class dijkstra {
  struct edge {
    int to;
    CostType cost;

    edge(int to_, CostType cost_) : to(to_), cost(cost_) {}
  };

  const CostType INF = numeric_limits<CostType>::max();

  int n;
  vector< vector<edge> > G;
  vector< CostType > ds;

  public:
  dijkstra(int n_) : n(n_), G(n_) {}

  void add_edge(int from, int to, CostType cost) {
    G[from].emplace_back(to, cost);
  }

  CostType inf() {
    return INF;
  }

  void build(int s) {
    using P = pair<CostType, int>;
    priority_queue< P, vector<P>, greater<P> > pq;

    ds.assign(n, INF);
    ds[s] = 0;
    pq.emplace(ds[s], s);

    while (!pq.empty()) {
      int v;
      CostType d;

      tie(d, v) = pq.top();
      pq.pop();

      if (ds[v] < d) continue;

      for (const edge &e : G[v]) {
        int u = e.to;
        if (ds[u] > ds[v] + e.cost) {
          ds[u] = ds[v] + e.cost;
          pq.emplace(ds[u], u);
        }
      }
    }
  }

  CostType dist(int to) {
    return ds[to];
  }

  CostType operator[](int k) {
    return dist(k);
  }
};
// }}}

void solve() {
  int r, c;
  cin >> r >> c;

  auto as = make_vector(r, c - 1, i64(0));
  cin >> as;

  auto bs = make_vector(r - 1, c, i64(0));
  cin >> bs;

  auto to_v = [&](int y, int x) {
    return y * c + x;
  };

  dijkstra<int> G(to_v(r, c));
  range(y, 0, r) range(x, 0, c - 1) {
    G.add_edge(to_v(y, x), to_v(y, x + 1), as[y][x]);
    G.add_edge(to_v(y, x + 1), to_v(y, x), as[y][x]);
  }

  range(y, 0, r - 1) range(x, 0, c) {
    G.add_edge(to_v(y, x), to_v(y + 1, x), bs[y][x]);
  }

  range(y, 0, r) range(x, 0, c) {
    range(i, 1, y + 1) {
      G.add_edge(to_v(y, x), to_v(y - i, x), i + 1);
    }
  }

  G.build(to_v(0, 0));
  i64 ans = G[to_v(r - 1, c - 1)];
  cout << ans << endl;
}

signed main() {
  solve();
}

//clear adj and visited vector declared globally after each test case
//check for long long overflow
//while adding and subs check if mod becomes -ve
//while using an integer directly in a builtin function add ll
//Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod;
//Dont keep array name as size or any other key word
//Incase of close mle change language to c++17 or c++14 

#include <bits/stdc++.h>  
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);cout.precision(dbl::max_digits10);
#define pb push_back
#define mod 1000000007ll //998244353ll
#define lld long double
#define mii map<int, int>
#define mci map<char, int>
#define msi map<string, int>
#define pii pair<int, int>
#define ff first
#define ss second 
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)    
#define fill(a,b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define print2d(dp,n,m) for(int i=0;i<=n;i++){for(int j=0;j<=m;j++)cout<<dp[i][j]<<" ";cout<<"\n";}
typedef std::numeric_limits< double > dbl;
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
const long long N=200005, INF=2000000000000000000;
lld pi=3.1415926535897932;
int lcm(int a, int b)
{
    int g=__gcd(a, b);
    return a/g*b;
}
int power(int a, int b)
    {
        if(a==0)
        return 0;
        int res=1;
        //a%=p;
        while(b>0)
        {
            if(b&1)
            res=(res*a);
            b>>=1;
            a*=a;
        }
        return res;
    }

int32_t main()
{
    IOS;
    int n;
    cin>>n;
    string s;
    cin>>s;
    string ss="110";
    int f1=1, f2=1, f3=1;
    for(int i=0;i<n;i++)
    {
        if(s[i]!=ss[i%3])
        f1=0;
        if(s[i]!=ss[(i+1)%3])
        f2=0;
        if(s[i]!=ss[(i+2)%3])
        f3=0;
    }
    int ans=0;
    int num=power(10, 10);
    //cout<<f1<<" "<<f2<<" "<<f3<<" "<<num<<"\n";
    int l1=(n/3), l2=(n/3), l3=(n/3);
    if(n%3==0)
    l1--;
    if(n%3==2)
    l3++;
    if(f1)
    ans+=(num-l1);
    if(f2)
    ans+=(num-l2);
    if(f3)
    ans+=(num-l3);
    cout<<ans<<"\n";
}