#include<bits/stdc++.h>
using namespace std;
int node,edge,z=0;
vector<int>adj_list[20];
map<char,int>mp;
map<int,char>rev_mp;
int vis[20];
void DFS(int curr,int dest)
{
vis[curr]=1;
if(curr==dest)
{
z=1;
return ;
}
for(int i=0;i<adj_list[curr].size();i++)
{
int child=adj_list[curr][i];
if(vis[child]==1&& z==0)
{
DFS(child,dest);
}
}
return ;
}
int main()
{
int id=0;
cin>>node>>edge;
for(int i=1;i<=edge;i++)
{
string e;
cin>>e;
if(mp.find(e[0])==mp.end())
{
mp[e[0]]=id;
rev_mp[id]=e[0];
id++;
}
if(mp.find(e[1])==mp.end())
{
mp[e[1]]=id;
rev_mp[id]=e[1];
id++;
}
adj_list[mp[e[0]]].push_back(mp[e[1]]);
}
char start,dest;
cin>>start>>dest;
for(int i=0;i<=node;i++)
{
vis[i]=0;
}
DFS(mp[start],mp[dest]);
cout<<z<<endl;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IG5vZGUsZWRnZSx6PTA7CnZlY3RvcjxpbnQ+YWRqX2xpc3RbMjBdOwptYXA8Y2hhcixpbnQ+bXA7Cm1hcDxpbnQsY2hhcj5yZXZfbXA7CmludCB2aXNbMjBdOwp2b2lkIERGUyhpbnQgY3VycixpbnQgZGVzdCkKewp2aXNbY3Vycl09MTsKaWYoY3Vycj09ZGVzdCkKewp6PTE7CnJldHVybiA7Cn0KZm9yKGludCBpPTA7aTxhZGpfbGlzdFtjdXJyXS5zaXplKCk7aSsrKQp7CmludCBjaGlsZD1hZGpfbGlzdFtjdXJyXVtpXTsKaWYodmlzW2NoaWxkXT09MSYmIHo9PTApCnsKREZTKGNoaWxkLGRlc3QpOwp9Cn0KcmV0dXJuIDsKfQppbnQgbWFpbigpCnsKaW50IGlkPTA7CmNpbj4+bm9kZT4+ZWRnZTsKZm9yKGludCBpPTE7aTw9ZWRnZTtpKyspCnsKc3RyaW5nIGU7CmNpbj4+ZTsKaWY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