//#include<bits/stdc++.h>
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<cstring>
#include<string>
#include<set>
//#include<unordered_map>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,b,a) for(int i=b;i>=a;i--)
#define m_p make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define pi acos(-1)
#define IO ios::sync_with_stdio(false);cin.tie(0)
#define io ios::sync_with_stdio(false)
typedef  long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll>pll;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const double  eps=1e-9;
inline int gcd(int a,int b){return b?gcd(b,a%b):a;}
inline int lcm(int a,int b){return a*b/gcd(a,b);}
inline int rd()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar();}
while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
return x*f;
}
const int maxn=1e5+50;
void run()
{
    int a, b, c;
    cin >> a >> b >> c;
    cout << 21 - a - b - c << endl;
}

int main()
{
   run();
   return 0;
}
/* 
start time:
over time:
*/

#include<bits/stdc++.h>
using namespace std;
//#include<atcoder/all>
//using namespace atcoder;
using ll = long long;
#define For(i,n,k) for(int i=(n);i<(k);i++)
#define ALL(a) (a).begin(),(a).end()
struct edge{
    int to;
    char c;
};
void Main(){
    int n, m;
    cin >> n >> m;
    vector<vector<edge>> graph(n);
    For(i,0,m){
        int a, b;
        char c;
        cin >> a >> b >> c;
        a--;
        b--;
        graph[a].push_back({b, c});
        if(a != b) graph[b].push_back({a, c});
    }
    vector<vector<int>> exGraph(n * n);
    For(i,0,n){
        For(j,0,n){
            for(auto [to1, c1]: graph[i]){
                for(auto [to2, c2]: graph[j]){
                    if(c1 == c2){
                        exGraph[i * n + j].push_back(to1 * n + to2);
                    }
                }
            }
        }
    }
    vector<int> dist(n * n, 1e9);
    dist[n - 1] = 0;
    queue<int> que;
    que.push(n - 1);
    while(!que.empty()){
        auto now = que.front();
        que.pop();
        for(auto to: exGraph[now]){
            if(dist[to] > dist[now] + 1){
                dist[to] = dist[now] + 1;
                que.push(to);
            }
        }
    }
    int ans = 1e9;
    For(i,0,n){
        ans = min(ans, dist[i * n + i] * 2);
        for(auto [to, c]: graph[i]){
            ans = min(ans, dist[i * n + to] * 2 + 1);
        }
    }
    if(ans == 1e9) ans = -1;
    cout << ans << endl;
}
int main(){
    Main();
    /*
    東方風神録は神が出てくるので当然神ゲー
    */
    return 0;
}