#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long int ll;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for(auto it = d.b;it != d.e;++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int a, b;cin >> a >> b;
  double ans = double(a - b) / (double)a;
  printf("%.9f\n", ans * 100.0);
}

/*
  Author     : MatsuTaku
  Date         : 03/27/21
  Certificate:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDF9T447OEo8XSQ6O1AznN5tKC8mzvYc4Zs3+oOKfMqgLXfOwQnpfcoxKs4MAP1eICBD13PkcIezJ9IlV6nKQZKs1BQmvjSXJ+zKK8YCKvAueNPuNO0Bim43IBaNHNFWcMvcmUvHgRb6hUSO0V8I7OsjiFo20KDBj3gAznu9tir0Q== CompetitiveProgrammingCertification:tkm.matsumoto@gmail.com
*/

#include <bits/stdc++.h>
using namespace std;
#include <x86intrin.h>
#define REP(i, f, n)      for (auto i = (f), i##_len = (n); i < i##_len; i++)
#define rep(i, n)         REP(i, (int) 0, n)
#define RREP(i, f, n)     for (auto i = (n)-1, i##_left = (f); i >= i##_left; i--)
#define rrep(i, n)        RREP(i, (int) 0, n)
using lint = long long int;
using ulint = unsigned long long int;
template<typename T>
using vvec = vector<vector<T>>;
template<typename T>
vvec<T> make_vvec(int n, int m, T v) { return vvec<T>(n, vector<T>(m, v)); }
template<typename T>
T& chmax(T& dst, T x) { return dst = max(dst, x); }
template<typename T>
T& chmin(T& dst, T x) { return dst = min(dst, x); }

class Solver {
 public:
  Solver();
  void solve();
};

Solver::Solver() {}

void Solver::solve() {
  int n,m; cin>>n>>m;
  vector<pair<int,char>> G[n];
  vector<pair<int,int>> E(m);
  auto id = [n](int a, int b) {
    if (a > b) swap(a,b);
    return a*n+b;
  };
  rep(i, m) {
    int a,b; char c; cin>>a>>b>>c;
    a--; b--;
    G[a].emplace_back(b,c-'a');
    G[b].emplace_back(a,c-'a');
    E[i] = {a,b};
  }
  vector<array<vector<int>, 26>> T(n);
  rep(i, n) {
    for (auto [t,c] : G[i]) {
      T[i][c].push_back(t);
    }
  }
  vector<int> W[n*n];
  rep(i, n) {
    REP(j, i, n) {
      auto s = id(i, j);
      rep(k, 26) {
        auto& l = T[i][k];
        auto& r = T[j][k];
        if (l.empty() or r.empty()) continue;
        for (auto u : l) {
          for (auto v : r) {
            auto t = id(u, v);
            W[s].push_back(t);
          }
        }
      }
    }
  }
  queue<int> qs;
  constexpr int INF = 1e9;
  vector<int> dist(n*n, INF);
  qs.push(id(0, n-1));
  dist[id(0, n-1)] = 0;
  while (!qs.empty()) {
    auto s = qs.front(); qs.pop();
    auto d = dist[s];
    for (auto t:W[s]) {
      if (dist[t] <= d+1) continue;
      dist[t] = d+1;
      qs.push(t);
    }
  }
  int ans = INF;
  rep(i, n) {
    chmin(ans, dist[id(i, i)]*2);
  }
  for (auto [a,b] : E) {
    chmin(ans, dist[id(a, b)]*2+1);
  }
  cout << (ans != INF ? ans : -1) << endl;
}

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  cout<<fixed<<setprecision(10);
  Solver solver;
  int t = 1;
//  cin>>t;
  while (t--) {
    solver.solve();
  }
  return 0;
}
