#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
using Graph = vector<vector<int>>;

int main(){
  int h,w;
  cin >> h >> w;
  vector<vector<char>> s(h+2, vector<char>(w+2, '.'));
  for(int i=1;i<=h;i++)
    for(int j=1;j<=w;j++)
      cin >> s[i][j];
  
  int ans = 0;
  for(int i=0;i<=h;i++){
    int c=0;
    for(int j=1;j<=w;j++)
    {
      if(s[i][j]==s[i+1][j]){
        c = 0;
      }else{
        if(c==0) ans++;
        c++;
      }
    }
  }
  
  for(int j=0;j<=w;j++){
    int c = 0;
    for(int i=1;i<=h;i++){
      if(s[i][j]==s[i][j+1]){
        c = 0;
      }else{
        if(c==0) ans++;
        c++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
#include <bits/stdc++.h>

#define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second

using namespace std;

typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;

template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
T div_floor(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T>
T div_ceil(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a > 0 ? (a - 1) / b + 1 : a / b;
}

template <typename T>
struct coord_comp {
    vector<T> v;
    bool sorted = false;

    coord_comp() {}

    int size() { return v.size(); }

    void add(T x) { v.push_back(x); }

    void build() {
        sort(v.begin(), v.end());
        v.erase(unique(v.begin(), v.end()), v.end());
        sorted = true;
    }

    int get_idx(T x) {
        assert(sorted);
        return lower_bound(v.begin(), v.end(), x) - v.begin();
    }
};

constexpr lint mod = 1000000007;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    vector<int> G[n][26];
    bool E[n][n];
    rep(i, n) rep(j, n) E[i][j] = false;
    rep(i, m) {
        int a, b;
        char c;
        scanf("%d%d %c", &a, &b, &c);
        --a;
        --b;
        G[a][c - 'a'].push_back(b);
        G[b][c - 'a'].push_back(a);
        E[a][b] = E[b][a] = true;
    }
    rep(i, n) rep(c, 26) {
        sort(G[i][c].begin(), G[i][c].end());
        G[i][c].erase(unique(G[i][c].begin(), G[i][c].end()), G[i][c].end());
    }

    int d[n][n];
    rep(i, n) rep(j, n) d[i][j] = mod;
    d[0][n - 1] = 0;
    queue<pii> que;
    que.emplace(0, n - 1);
    while (!que.empty()) {
        auto [u, v] = que.front();
        if (E[u][v]) {
            printf("%d\n", d[u][v] + 1);
            return 0;
        }
        que.pop();
        rep(c, 26) for (auto nu : G[u][c]) for (auto nv : G[v][c]) {
            if (chmin(d[nu][nv], d[u][v] + 2)) {
                if (nu == nv) {
                    printf("%d\n", d[nu][nv]);
                    return 0;
                }
                que.emplace(nu, nv);
            }
        }
    }
    puts("-1");
}