import java.io.BufferedInputStream;
import java.util.*;
class word{
String s;
String f;
public word(String s,String f){
this.s = s;
this.f = f;
}
}
class pair{
String first;
int second;
public pair(String first,int second){
this.first = first;
this.second = second;
}
}
public class Main {
static int N = 10;
static String st,fl;
static word[] wd = new word[N];
public static void main(String[] args){
Scanner sc = new Scanner(new BufferedInputStream(System.in));
st = sc.next();
fl = sc.next();
int idx = 0;
sc.nextLine();
// 输入数据
String a = null,b = null;
String str;
while(sc.hasNextLine()){
str = sc.nextLine();
if(str.isEmpty())
break;
String[] s = str.split(" ");
a = s[0];
b = s[1];
wd[idx ++] = new word(a,b);
}
Queue<pair> queue = new LinkedList<>();
queue.add(new pair(st,0));
String cur,New;
while(!queue.isEmpty()){
pair pair = queue.poll();
cur = pair.first;
int cnt = pair.second;
// 判断
if(cnt > 10){
System.out.println("NO ANSWER!");
return;
}
if(cur.equals(fl)){
System.out.println(cnt);
return;
}
// 直接枚举全部的变换方法 如果可以变化的话便对其来进行变化 并且把这种变化加入到队列当中取
for(int i = 0;i < idx;i ++){
if(cur.indexOf(wd[i].s) != -1){
//直接使用replace大法
New = cur.replaceFirst("\\Q" + wd[i].s + "\\E", wd[i].f);
queue.add(new pair(New,cnt + 1));
}
}
}
}
}