#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(),v.end()
#define pb push_back
template<class T,class U>
inline bool chmax(T &a,U b){
    if(a<b){
        a=b;
        return true;
    }
    return false;
}
template<class T,class U>
inline bool chmin(T &a,U b){
    if(a>b){
        a=b;
        return true;
    }
    return false;
}
constexpr int INF=1000000000;
constexpr ll llINF=3000000000000000000;
constexpr int mod=1000000007;
constexpr double eps=1e-8;
const double pi=acos(-1);
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int Random(int mi,int ma){
    random_device rnd;
    mt19937 mt(rnd());//32bit
    //[mi,ma]
    uniform_int_distribution<int>engine(mi,ma);
    return engine(mt);
}
/*
vector<vector<ll>>C,sC;
void init_comb(int n,int m){
    C.resize(n+1,vector<ll>(m+1,0));
    sC.resize(n+1,vector<ll>(m+1,0));
    C[0][0]=1;
    for(int i=1;i<=n;i++){
        C[i][0]=1;
        for(int j=1;j<=m;j++){
            C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
        }
    }
    rep(i,n+1){
        rep(j,m){
            sC[i][j+1]=(sC[i][j]+C[i][j])%mod;
        }
    }
}*/
ll gcd(ll a,ll b){
    while(a%b){
        a%=b;
        swap(a,b);
    }
    return b;
}
ll lcm(ll a,ll b){
    return a/gcd(a,b)*b;
}
bool isprime(int a){
    if(a==1)return false;
    for(int i=2;i*i<=a;i++){
        if(a%i==0)return false;
    }
    return true;
}
vector<int>primes;
void init_prime(int n){
	primes.push_back(2);
    for(int i=3;i<=n;i+=2){
        bool f=true;
        for(int j:primes){
            if(j*j>i)break;
            if(i%j==0){
                f=false;
                break;
            }
        }
        if(f)primes.push_back(i);
    }
}
ll modpow(ll a,ll b){
    ll res=1;
    while(b){
        if(b&1){
            res*=a;
            res%=mod;
        }
        a*=a;
        a%=mod;
        b>>=1;
    }
    return res;
}
vector<ll>inv,fact,factinv;
void init_fact(int n){
    inv.resize(n+1);
    fact.resize(n+1);
    factinv.resize(n+1);
    inv[0]=0;
    inv[1]=1;
    fact[0]=1;
    factinv[0]=1;
    for(ll i=1;i<=n;i++){
        if(i>=2)inv[i]=mod-((mod/i)*inv[mod%i]%mod);
        fact[i]=(fact[i-1]*i)%mod;
        factinv[i]=factinv[i-1]*inv[i]%mod;
    }
}
ll _inv(ll a,ll m=mod){
    //gcd(a,m) must be 1
    ll b=m,u=1,v=0;
    while(b){
        ll t=a/b;
        a-=t*b;swap(a,b);
        u-=t*v;swap(u,v);
    }
    u%=m;
    if(u<0)u+=m;
    return u;
}
ll comb(int a,int b){
    if(a<b)return 0;
    if(a<0)return 0;
    return fact[a]*factinv[a-b]%mod*factinv[b]%mod;
}
ll multicomb(int a,int b){
    return comb(a+b-1,b);
}
int n,m;
vector<int>g[1010][26];
int dp[1010][1010];
#define P pair<int,int>
#define PP pair<int,P>
void solve(){
    cin>>n>>m;
    priority_queue<PP,vector<PP>,greater<PP>>q;
    rep(i,n)rep(j,n)dp[i][j]=INF;
    rep(i,n){
        dp[i][i]=0;
        q.push({0,{i,i}});
    }
    rep(i,m){
        int a,b;
        char c;
        cin>>a>>b>>c;
        a--,b--;
        g[a][c-'a'].push_back(b);
        g[b][c-'a'].push_back(a);
        if(a!=b){
            dp[a][b]=dp[b][a]=1;
            q.push({1,{a,b}});
            q.push({1,{b,a}});
        }
    }
    while(!q.empty()){
        PP p=q.top();
        q.pop();
        int l=p.second.first,r=p.second.second;
        if(dp[l][r]!=p.first)continue;
        rep(k,26){
            for(int a:g[l][k]){
                for(int b:g[r][k]){
                    if(chmin(dp[a][b],p.first+2)){
                        q.push({dp[a][b],{a,b}});
                    }
                }
            }
        }
    }
    cout<<(dp[0][n-1]==INF?-1:dp[0][n-1])<<endl;
}
int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    //int t;cin>>t;while(t--)
    solve();
    return 0;
}
/***
 
                                Bismillahir Rahmanir Rahim
                                Rabbi Zidni Ilma
                                Author : Saidur Rahman
                                Department of CSE, City University, Bangladesh.
 
***/
 
#include<bits/stdc++.h>
using namespace std;
 
 
 
#define         endl                "\n"
#define         EPS                 1e-6
#define         F                   first
#define         sf                  scanf
#define         pf                  printf
#define         S                   second
#define         done                return 0
#define         MP                  make_pair
#define         pb                  push_back
#define         start               int main()
#define         PI                  acos(-1.0)
#define         NO                  cout<<"NO\n"
#define         No                  cout<<"No\n"
#define         no                  cout<<"no\n"
#define         YES                 cout<<"YES\n"
#define         Yes                 cout<<"Yes\n"
#define         yes                 cout<<"yes\n"
#define         sz(x)               int(x.size())
#define         all(a)              a.begin(),a.end()
#define         srt(x)              sort(x.begin(),x.end())
#define         clock_start         clock_t tStart = clock()
#define         MEM(a, b)           memset(a, (b), sizeof(a))         
#define         watch(x)            cout << (#x) << " is " << (x) << endl
#define         Fast                ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define         show_time           cout<<"\n\nTime Taken: "<<fixed<<setprecision(3)<<(double)(clock() - tStart) /  CLOCKS_PER_SEC<<"s"
 

            /****** Template of some bit operations *****/

long long     ToggleBit (long long n, long long X)    { return n ^ (1LL << X); }//k-1 tomo bit 1 hole 0 korbe r 0 hole 1
long long     SetBit (long long n, long long X)       { return n | (1LL << X); }//k-1 tomo bit 0 hole 1 korbe, 1 hole unchange
long long     ClearBit (long long n, long long X)     { return n & ~(1LL << X); }//k-1 tomo bit 1 hole 0 korbe, 0 hole unchange
bool          CheckBit (long long n, long long X)     { return (bool)(n & (1LL << X)); }//k-1 tomo bit 1 naki 0 bolbe
 

            ///////////////////////////////////////////////////////////////////////////
        ///////////////////////// DO NOT TOUCH BEFORE THIS LINE //////////////////////////
            //////////////////////////////////////////////////////////////////////////
 
 
typedef     long long   ll;
const       int MAX  = 1e5 + 5;
const       int MOD  = 1e9 + 7;
const       int INF  = 1e9;
const       ll LINF  = 1e18;





start
{
    Fast;

#ifndef ONLINE_JUDGE
    clock_start;
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    string s1, s2;
    cin >> s1 >> s2;
    int cnt1=0, cnt2=0;
    for (int i = 0; i < sz(s1);i++)
    {
        cnt1 += (s1[i] - '0');
    }
    for (int i = 0; i < sz(s2);i++)
    {
        cnt2 += (s2[i] - '0');
    }
    cout << max(cnt1, cnt2) << endl;

#ifndef ONLINE_JUDGE 
    show_time;
#endif

    done;
}
