#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < n; i++)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define INF 1e9
typedef long long ll;
typedef long double ld;

map<ll, ll> makeMap(vector<ll> v);

int main(){
  ll n;
  cin>>n;
  vector<ll> a(n), b(n), c(n);
  REP(i,n)cin>>a[i];
  REP(i,n)cin>>b[i];
  REP(i,n)cin>>c[i];
  map<ll, ll> A, C;
  A = makeMap(a);
  C = makeMap(c);
  map<ll, set<ll>> B;
  REP(i,n){
    if(B.count(b[i]) > 0)B.at(b[i]).insert(i);
    else B.insert({b[i], {i}});
  }
  ll ans = 0;
  for(auto ap: A){
    ll anum = ap.first;
    if(B.count(anum) > 0){
      set<ll> s = B.at(anum);
      for(auto itr: s){
        if(C.count(itr + 1) > 0)ans += ap.second * C.at(itr + 1);
      }
    }
  }
  cout<<ans<<endl;
  return 0;
}

map<ll,ll> makeMap(vector<ll> v){
  ll n = v.size();
  map<ll, ll> mp;
  REP(i,n){ 
    if(mp.count(v[i]) > 0)mp.at(v[i])++;
    else mp.insert(make_pair(v[i], 1));
  }
  return mp;
}
#include <iostream>
#include <queue>
using namespace std;

template <typename T>
struct Edge
{
    int to;
    T cost;
};

template <typename T>
struct WeightedGraph
{
    int n;
    std::vector<std::vector<Edge<T>>> g;
    
    WeightedGraph(){}
    
    WeightedGraph(int n) : n(n){
        g.resize(n);
    }
    
    void add_edge(int from, int to, T cost){
        g[from].push_back((Edge<T>){to, cost});
    }
};

template <typename T>
std::vector<T> dijkstra(WeightedGraph<T> &g, int s){
    int n = g.n;
    std::vector<T> d(n);
    fill(d.begin(), d.end(), -1);
    std::priority_queue<std::pair<T, int>, std::vector<std::pair<T, int>>, std::greater<std::pair<T, int>>> que;
    d[s] = 0;
    que.push(std::pair<T, int>(0, s));
    while(que.size()){
        std::pair<T, int> p = que.top();
        que.pop();
        int u = p.second;
        if(d[u] < p.first) continue;
        for(Edge<T> &e : g.g[u]){
            int v = e.to;
            if(d[v] == -1 || d[v] > d[u] + e.cost){
                d[v] = d[u] + e.cost;
                que.push(std::pair<T, int>(d[v], v));
            }
        }
    }
    return d;
}

int main()
{
    int r, c;
    cin >> r >> c;
    WeightedGraph<int> g(r * c * 2);
    for(int i = 0; i < r; i++){
        for(int j = 0; j < c - 1; j++){
            int a;
            cin >> a;
            int u = i * c + j, v = i * c + j + 1;
            g.add_edge(u, v, a);
            g.add_edge(v, u, a);
        }
    }
    for(int i = 0; i < r - 1; i++){
        for(int j = 0; j < c; j++){
            int b;
            cin >> b;
            int u = i * c + j, v = (i + 1) * c + j;
            g.add_edge(u, v, b);
        }
    }
    for(int i = 1; i < r; i++){
        for(int j = 0; j < c - 1; j++){
            int u = i * c + j, v = (i - 1) * c + j;
            g.add_edge(u, v + r * c, 2);
            g.add_edge(u + r * c, v + r * c, 1);
        }
    }
    for(int i = 0; i < r; i++){
        for(int j = 0; j < c; j++){
            int u = i * c + j;
            g.add_edge(u + r * c, u, 0);
        }
    }
    vector<int> d = dijkstra(g, 0);
    cout << d[r * c - 1] << endl;
}
