#include<bits/stdc++.h>
using namespace std;
int node,edge,zaoa_zay=0;
vector<int>adj_list[20];
map<char,int>mp;
map<int,char>rev_mp;
int vis[20];
void DFS(int curr,int dst)
{
vis[curr]=1;
if(curr==dst)
{
zaoa_zay=1;
return ;
}
for(int i=0;i<adj_list[curr].size();i++)
{
int child=adj_list[curr][i];
if(vis[child]==0 && zaoa_zay==0)
{
DFS(child,dst);
}
}
return;
}
int main()
{
int id=0;
int node,edge;
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 st,dst;
cin>>st>>dst;
for(int i=0;i<=node;i++)
{
vis[i]=0;
}
DFS(mp[st],mp[dst]);
cout<<zaoa_zay<<endl;
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IG5vZGUsZWRnZSx6YW9hX3pheT0wOwp2ZWN0b3I8aW50PmFkal9saXN0WzIwXTsKbWFwPGNoYXIsaW50Pm1wOwptYXA8aW50LGNoYXI+cmV2X21wOwppbnQgdmlzWzIwXTsKdm9pZCBERlMoaW50IGN1cnIsaW50IGRzdCkKewp2aXNbY3Vycl09MTsKaWYoY3Vycj09ZHN0KQp7Cnphb2FfemF5PTE7CnJldHVybiA7Cn0KZm9yKGludCBpPTA7aTxhZGpfbGlzdFtjdXJyXS5zaXplKCk7aSsrKQp7CmludCBjaGlsZD1hZGpfbGlzdFtjdXJyXVtpXTsKaWYodmlzW2NoaWxkXT09MCAmJiB6YW9hX3pheT09MCkKewpERlMoY2hpbGQsZHN0KTsKfQp9CnJldHVybjsKfQppbnQgbWFpbigpCnsKaW50IGlkPTA7CmludCBub2RlLGVkZ2U7CmNpbj4+bm9kZT4+ZWRnZ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