#include <bits/stdc++.h>

using namespace std;

vector<pair<int, int>> edges[26];
vector<pair<int, int>> states[1024][1024];
int d[1024][1024];

int main()
{
  int N, M;
  
  scanf("%d %d", &N, &M);
  
  queue<pair<int, int>> q;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      d[i][j] = 1'000'000'000;
    }
    d[i][i] = 0; q.emplace(i, i);
  }
  
  for (int i = 0; i < M; i++) {
    int u, v; char c[2];
    scanf("%d %d %s", &u, &v, c);
    if (u > v) swap(u, v);
    edges[c[0] - 'a'].emplace_back(u, v);
    if (u != v) d[u][v] = d[v][u] = 1;
    q.emplace(u, v);
  }
  
  for (int a = 0; a < 26; a++) {
    int sz = edges[a].size();
    for (int i = 0; i < sz; i++) {
      for (int j = i + 1; j < sz; j++) {
        auto [u, v] = edges[a][i];
        auto [w, x] = edges[a][j];
        states[u][w].emplace_back(v, x); states[w][u].emplace_back(v, x);
        states[u][x].emplace_back(v, w); states[x][u].emplace_back(v, w);
        states[v][w].emplace_back(u, x); states[w][v].emplace_back(u, x);
        states[v][x].emplace_back(u, w); states[x][v].emplace_back(u, w);
      }
    }
  }
  
  while (q.size()) {
    auto [u, v] = q.front(); q.pop();
    if (u > v) swap(u, v);
    if (u == 1 && v == N) {
      printf("%d\n", d[u][v]); return 0;
    }
    for (auto [u2, v2] : states[u][v]) {
      if (d[u2][v2] == 1'000'000'000) {
        d[u2][v2] = d[v2][u2] = d[u][v] + 2;
        q.emplace(u2, v2);
      }
    }
  }
  
  printf("-1\n");
  
  return 0;
}
#include <bits/stdc++.h>
using namespace std;
/*
#include <atcoder/all>
using namespace atcoder;
using mint = modint1000000007;
*/
#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define endl "\n"

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {os << "["; for (const auto &v : vec) {os << v << ","; } os << "]"; return os;}
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) {os << "(" << p.first << ", " << p.second << ")"; return os;}
ll mod_pow(ll a, ll n, ll mod) { ll ret = 1; ll p = a % mod; while (n) { if (n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; }

const int mod = 1e9 + 7;

bool check(vector<vector<int>> &A, int K, int c) {
    int N = A.size();
    vector<vector<int>> B(N, vector<int>(N));
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            B[i][j] = (A[i][j] <= c);
        }
    }
    vector<vector<int>> acc(N + 1, vector<int>(N + 1));
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= N; j++) {
            acc[i][j] = acc[i - 1][j] + acc[i][j - 1] - acc[i - 1][j - 1] + B[i - 1][j - 1];
        }
    }
    for (int i = 0; i <= N - K; i++) {
        for (int j = 0; j <= N - K; j++) {
            int sum = acc[i + K][j + K] - acc[i][j + K] - acc[i + K][j] + acc[i][j];
            if (sum >= (K * K + 1) / 2) return true;
        }
    }
    return false;
}

void solve() {
    int N, K;
    cin >> N >> K;
    vector<vector<int>> A(N, vector<int>(N));
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cin >> A[i][j];
        }
    }
    int ok = 1e9 + 1, ng = -1;
    while(ok - ng > 1) {
        int c = (ok + ng) / 2;
        if (check(A, K, c)) {
            ok = c;
        } else {
            ng = c;
        }
    }
    cout << ok << endl;
}

int main() {
    #ifdef LOCAL_ENV
    cin.exceptions(ios::failbit);
    #endif
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(16);
    
    solve();
}