#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
int main() {
int n = 0;
cin >> n;
string str1, str2;
cin >> str1 >> str2;
vector<string> str;
for (int i = 0; i < n; i++) {
string tmp;
cin >> tmp;
str.push_back(tmp);
}
int minDist = n; // 初始化最小距离为数组长度
int index1 = -1, index2 = -1; // 记录最近出现的索引
for (int i = 0; i < n; i++) {
if (str[i] == str1) {
index1 = i;
if (index2 != -1) {
minDist = min(minDist, abs(index1 - index2));
}
} else if (str[i] == str2) {
index2 = i;
if (index1 != -1) {
minDist = min(minDist, abs(index1 - index2));
}
}
}
if (index1 == -1 || index2 == -1) {
cout << -1;
} else if (minDist == n) {
cout << -1;
} else {
cout << minDist;
}
return 0;
}