import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static Scanner sc = new Scanner(System.in);
public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws Exception {
String nStr = reader.readLine();
int n = Integer.valueOf(nStr);
String s12 = reader.readLine();
String[] sArr = s12.split(" ");
String[] strs = new String[n];
for(int i=0;i<n;i++){
strs[i] = reader.readLine();
}
if(n <= 1 || sArr.length <= 1){
System.out.println(-1);
return;
}
String s1 = sArr[0];
String s2 = sArr[1];
int lastS1Pos = -1;
int lastS2Pos = -1;
int ans = -1;
for(int i=0;i<n;i++){
if(strs[i].equals(s1)){
if(lastS2Pos != -1){
// 计算完并清除s2
if(ans == -1) {
ans = i - lastS2Pos;
}else {
ans = Math.min(ans, i - lastS2Pos);
}
lastS2Pos = -1;
lastS1Pos = i;
}else {
lastS1Pos = i;
}
}
if(strs[i].equals(s2)){
if(lastS1Pos != -1){
// 计算完并清除 s1
if(ans == -1) {
ans = i - lastS1Pos;
}else {
ans = Math.min(ans, i - lastS1Pos);
}
lastS1Pos = -1;
lastS2Pos = i;
}else {
lastS2Pos = i;
}
}
}
System.out.println(ans);
}
}
/*
1
CD AB
CD
-1
5
QWER 666
QWER
1234
qwe
666
QWER
1
5
QWER kkkk
QWER
1234
qwe
666
QWER
*/