#include <bits/stdc++.h>

const long long INF = 1ll << 61;

using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  
  vector<vector<pair<int, int>>> edges(26);
  
  for (int e = 0; e < M; e++) {
    int A, B;
    char C;
    cin >> A >> B >> C;
    
    edges[C - 'a'].push_back({--A, --B});
  }
  
  vector<vector<int>> graph(N * N);
  
  auto index = [&](int u, int v) {
    return min(u, v) * N + max(u, v);
  };
  
  for (int ch = 0; ch < 26; ch++) {
    for (auto [u, v] : edges[ch]) {
      for (auto [w, x] : edges[ch]) {
        graph[index(u, w)].push_back(index(v, x));
        graph[index(u, x)].push_back(index(v, w));
        graph[index(v, w)].push_back(index(u, x));
        graph[index(v, x)].push_back(index(u, w));
      }
    }
  }
  
  priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
  
  vector<long long> dist(N * N, INF);
  
  pq.push({0ll, index(0, N - 1)});
  dist[index(0, N - 1)] = 0;
  
  while (!pq.empty()) {
    auto [cost, node] = pq.top();
    pq.pop();
    
    if (dist[node] < cost) continue;
    
    for (int next : graph[node]) {
      if (dist[next] <= cost + 1) continue;
      
      dist[next] = cost + 1;
      pq.push({dist[next], next});
    }
  }
  
  long long ans = INF;
  
  for (int i = 0; i < N; i++) {
    ans = min(dist[index(i, i)] * 2, ans);
  }
  
  for (int ch = 0; ch < 26; ch++) {
    for (auto [u, v] : edges[ch]) {
      ans = min(dist[index(u, v)] * 2 + 1, ans);
    }
  }
  
  cout << (ans < INF ? ans : -1) << endl;
  
  return 0;
}
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (ll i=(ll)N-1; i>=0; i--)
#define popcount __builtin_popcount

const ll LLINF = pow(2,61)-1;
const ll INF = pow(2,30)-1;

ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }

// ----------------------------------------------------------------------
// ----------------------------------------------------------------------

struct dijkstra {

  ll N;
  struct edge { ll to, d; };
  vector<vector<edge>> adj;
  vector<ll> minlen;

  dijkstra(ll n) { N = n; adj.resize(N); minlen.assign(N,LLINF); }
  ll operator[](const ll &x) const { return minlen[x]; }

  void add_edge(ll from, ll to, ll d){
    adj[from].push_back({to,d});
  }
  vector<ll> calc(ll n=0) {
    auto c = [](const p_ll &x, const p_ll &y){return x.second>y.second;};
    priority_queue<p_ll, vector<p_ll>, decltype(c)> q(c);
    minlen[n] = 0; q.push(make_pair(n,0));
    while(q.size()) {
      p_ll now = q.top(); q.pop();
      ll np = now.first, nd = now.second;
      if (nd>minlen[np]) continue;
      for (auto x: adj[np]) {
        if (minlen[x.to]<=minlen[np]+x.d) continue;
        q.push(make_pair(x.to, minlen[np]+x.d));
        minlen[x.to] = minlen[np]+x.d;
      }
    }
    return minlen;
  }

};

// ----------------------------------------------------------------------
// ----------------------------------------------------------------------

ll R, C;
ll gid(ll i, ll j, bool ura) { return i*C + j + ura*(R*C); }

int main() {
  cin >> R >> C;
  ll A[R][C-1]; rep(i,R) rep(j,C-1) cin >> A[i][j];
  ll B[R-1][C]; rep(i,R-1) rep(j,C) cin >> B[i][j];

  dijkstra ds(R*C*2);
  rep(i,R) rep(j,C-1) {
    ds.add_edge(gid(i,j,false), gid(i,j+1,false), A[i][j]);
    ds.add_edge(gid(i,j+1,false), gid(i,j,false), A[i][j]);
  }
  rep(i,R-1) rep(j,C) {
    ds.add_edge(gid(i,j,false), gid(i+1,j,false), B[i][j]);
    ds.add_edge(gid(i+1,j,true), gid(i,j,true), 1);
  }
  rep(i,R) rep(j,C) {
    ds.add_edge(gid(i,j,false), gid(i,j,true), 1);
    ds.add_edge(gid(i,j,true), gid(i,j,false), 0);
  }
  

  vector<ll> d = ds.calc();
  ll result = ds[gid(R-1,C-1,false)];
  cout << result << endl;
  return 0;
}