#include <bits/stdc++.h>
using namespace std;
int H[1 << 17][8];
int V[1 << 17][8];
int A[1 << 17];
int S[1 << 17];
int Pos[1 << 17];
int B[1 << 10];
int C[8];
int E[8][1 << 10][8];
long long R[2][1 << 10];
int Encode(int v[], int n) {
  int res = 0;
  int i;
  for (i = 0; i < n; i++) {
    res <<= 3;
    res |= v[i];
  }
  return res;
}
void Decode(int a, int n, int v[]) {
  int i;
  for (i = n - 1; i >= 0; i--) {
    v[i] = a & 7;
    a >>= 3;
  }
}
int F(int a, int n) {
  int v[8];
  Decode(a, n, v);
  int i, j;
  for (i = 0; i < n; i++) v[i] = -v[i];
  int cnt = 0;
  for (i = 0; i < n; i++)
    if (v[i] < 0) {
      ++cnt;
      int t = v[i];
      for (j = 0; j < n; j++)
        if (v[j] == t) v[j] = cnt;
    }
  return Encode(v, n);
}
int main() {
  int N, M;
  scanf("%d%d", &N, &M);
  int i, j, k, l;
  for (i = 0; i < M - 1; i++)
    for (j = 0; j < N; j++) scanf("%d", &H[i][j]);
  for (i = 0; i < M; i++)
    for (j = 0; j < N; j++) scanf("%d", &V[i][j]);
  int cnt = 0;
  for (i = 0; i < 1 << (N + N + N); i++) {
    A[i] = F(i, N);
    if (A[i] == i) {
      S[cnt] = i;
      Pos[i] = cnt;
      ++cnt;
    }
  }
  for (i = 0; i < cnt; i++) {
    int v[8];
    Decode(S[i], N, v);
    for (j = 0; j < N; j++)
      if (v[j] == 0) break;
    B[i] = j == N ? 0 : 1;
  }
  for (i = 0; i < N; i++) C[i] = 4;
  C[0] = 2;
  C[N - 1] = 8;
  for (i = 0; i < N; i++)
    for (j = 0; j < cnt; j++)
      for (k = 0; k < 8; k++) {
        int v[8];
        Decode(S[j], N, v);
        if ((k & 1) == 0) v[i] = 7;
        if ((k & 2) != 0 && i != 0 && v[i] != v[i - 1]) {
          int Min = min(v[i - 1], v[i]);
          int Max = max(v[i - 1], v[i]);
          for (l = 0; l < N; l++)
            if (v[l] == Max) v[l] = Min;
        }
        if ((k & 4) != 0 && i == N - 1 && v[i] != v[0]) {
          int Min = min(v[0], v[i]);
          int Max = max(v[0], v[i]);
          for (l = 0; l < N; l++)
            if (v[l] == Max) v[l] = Min;
        }
        E[i][j][k] = Pos[A[Encode(v, N)]];
      }
  long long res = (1LL << 60);
  for (i = 0; i < cnt; i++) R[0][i] = (1LL << 60);
  R[0][0] = 0;
  int prev = 0;
  int next = 1;
  for (i = 0; i < M - 1; i++)
    for (j = 0; j < N; j++) {
      for (k = 0; k < cnt; k++) R[next][k] = (1LL << 60);
      for (k = 0; k < cnt; k++)
        if (R[prev][k] != (1LL << 60) && B[k] != 0)
          for (l = 0; l < C[j]; l++) {
            int pos = E[j][k][l];
            long long r = R[prev][k];
            if ((l & 1) == 0) r += H[i][j];
            if ((l & 2) == 0 && j != 0) r += V[i + 1][j - 1];
            if ((l & 4) == 0 && j == N - 1) r += V[i + 1][j];
            R[next][pos] = min(R[next][pos], r);
            if (B[pos] == 0) res = min(res, r);
          }
      swap(prev, next);
    }
  printf("%lld\n", res);
  return 0;
};
