#include <iostream>
using namespace std;
string shuffle (string &s1, string &s2) {
string s;
for (int i = 0; i < s1.length(); i++) {
s += s2[i];
s += s1[i];
}
s1 = s.substr(0, s1.length());
s2 = s.substr(s2.length(), s2.length());
return s;
}
int main() {
string s1, s2, s;
int n;
while (cin >> n && n) {
cin >> s1 >> s2 >> s;
string s12 = s1; s12 += s2;
string fixS = s12;
bool flag = true;
int nofs = 0; // number of steps
do {
nofs++;
s12 = shuffle(s1, s2);
if (s == s12)
flag = false;
} while (s12 != fixS && flag && nofs <= 50);
cout << (flag ? -1 : nofs) << endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwogCnN0cmluZyBzaHVmZmxlIChzdHJpbmcgJnMxLCBzdHJpbmcgJnMyKSB7CglzdHJpbmcgczsKCWZvciAoaW50IGkgPSAwOyBpIDwgczEubGVuZ3RoKCk7IGkrKykgewoJCXMgKz0gczJbaV07CgkJcyArPSBzMVtpXTsKCX0KCXMxID0gcy5zdWJzdHIoMCwgczEubGVuZ3RoKCkpOwoJczIgPSBzLnN1YnN0cihzMi5sZW5ndGgoKSwgczIubGVuZ3RoKCkpOwoJcmV0dXJuIHM7Cn0gCiAKaW50IG1haW4oKSB7CglzdHJpbmcgczEsIHMyLCBzOwoJaW50IG47Cgl3aGlsZSAoY2luID4+IG4gJiYgbikgewoJCWNpbiA+PiBzMSA+PiBzMiA+PiBzOwoJCXN0cmluZyBzMTIgPSBzMTsgczEyICs9IHMyOwoJCXN0cmluZyBmaXhTID0gczEyOwoJCWJvb2wgZmxhZyA9IHRydWU7CgkJaW50IG5vZnMgPSAwOwkJCS8vIG51bWJlciBvZiBzdGVwcwoJCWRvIHsKCQkJbm9mcysrOwoJCQlzMTIgPSBzaHVmZmxlKHMxLCBzMik7CgkJCWlmIChzID09IHMxMikKCQkJCWZsYWcgPSBmYWxzZTsKCQl9IHdoaWxlIChzMTIgIT0gZml4UyAmJiBmbGFnICYmIG5vZnMgPD0gNTApOwoJCWNvdXQgPDwgKGZsYWcgPyAtMSA6IG5vZnMpIDw8IGVuZGw7Cgl9CglyZXR1cm4gMDsKfQ==