import java.io.*;
import java.math.BigInteger;
import java.util.*;
class CyclicHashes
implements Runnable {
}
public static void main
(String[] args
) { new Thread(null,
new CyclicHashes
(),
"", 1l
* 200 * 1024 * 1024).
start(); }
public void run() {
try {
init();
solve();
out.close();
e.printStackTrace();
}
}
int n;
int prime1, prime2;
int mod1, mod2;
int[] pow1, pow2;
int[] h1, h2;
void init(char[] s) {
prime1
= BigInteger.
valueOf(3000 + rnd.
nextInt(500)) .nextProbablePrime().intValue();
prime2
= BigInteger.
valueOf(4000 + rnd.
nextInt(500)) .nextProbablePrime().intValue();
mod1
= BigInteger.
valueOf((int) 1e9
+ rnd.
nextInt(500)) .nextProbablePrime().intValue();
mod2
= BigInteger.
valueOf((int) 1e9
+ (int) 1e6
+ rnd.
nextInt(500)) .nextProbablePrime().intValue();
n = s.length;
pow1 = new int[n];
pow2 = new int[n];
pow1[0] = pow2[0] = 1;
for (int i = 1; i < n; i++) {
pow1[i] = (int) ((long) pow1[i - 1] * prime1 % mod1);
pow2[i] = (int) ((long) pow2[i - 1] * prime2 % mod2);
}
h1 = new int[n];
h2 = new int[n];
h1[0] = h2[0] = s[0];
for (int i = 1; i < n; i++) {
h1[i] = (int) ((h1[i - 1] + (long) pow1[i] * s[i]) % mod1);
h2[i] = (int) ((h2[i - 1] + (long) pow2[i] * s[i]) % mod2);
}
}
long getHash(int L, int R) {
int hash1 = getHash(L, R, h1, pow1, mod1);
int hash2 = getHash(L, R, h2, pow2, mod2);
return ((long) hash1 << 32) | hash2;
}
int getHash(int L, int R, int[] h, int[] pow, int mod) {
int hash = h[R];
if (L > 0)
hash -= h[L - 1];
if (hash < 0)
hash += mod;
return (int) ((long) hash * pow[n - R - 1] % mod);
}
boolean equals(int L1, int R1, int L2, int R2) {
long hash1 = getHash(L1, R1);
long hash2 = getHash(L2, R2);
return hash1 == hash2;
}
int n
= Integer.
parseInt(in.
readLine()); StringBuilder sb = new StringBuilder();
sb.append(s);
sb.append(t);
sb.append(t);
char[] res = sb.toString().toCharArray();
init(res);
for (int shift = 0; shift < n; shift++) {
if (equals(0, n - 1, n + shift, 2 * n - 1 + shift)) {
out.println(shift);
return;
}
}
out.println(-1);
}
}
aW1wb3J0IGphdmEuaW8uKjsKaW1wb3J0IGphdmEubWF0aC5CaWdJbnRlZ2VyOwppbXBvcnQgamF2YS51dGlsLio7CgpjbGFzcyBDeWNsaWNIYXNoZXMgaW1wbGVtZW50cyBSdW5uYWJsZSB7CgogICAgcHJpdmF0ZSBCdWZmZXJlZFJlYWRlciBpbjsKICAgIHByaXZhdGUgUHJpbnRXcml0ZXIgb3V0OwoKICAgIHZvaWQgaW5pdCgpIHRocm93cyBGaWxlTm90Rm91bmRFeGNlcHRpb24gewogICAgICAgIGluID0gbmV3IEJ1ZmZlcmVkUmVhZGVyKG5ldyBJbnB1dFN0cmVhbVJlYWRlcihTeXN0ZW0uaW4pKTsKICAgICAgICBvdXQgPSBuZXcgUHJpbnRXcml0ZXIoU3lzdGVtLm91dCk7CiAgICB9CgogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIG5ldyBUaHJlYWQobnVsbCwgbmV3IEN5Y2xpY0hhc2hlcygpLCAiIiwgMWwgKiAyMDAgKiAxMDI0ICogMTAyNCkuc3RhcnQoKTsKICAgIH0KCiAgICBwdWJsaWMgdm9pZCBydW4oKSB7CiAgICAgICAgdHJ5IHsKICAgICAgICAgICAgaW5pdCgpOwogICAgICAgICAgICBzb2x2ZSgpOwogICAgICAgICAgICBvdXQuY2xvc2UoKTsKICAgICAgICB9IGNhdGNoIChFeGNlcHRpb24gZSkgewogICAgICAgICAgICBlLnByaW50U3RhY2tUcmFjZSgpOwogICAgICAgICAgICBTeXN0ZW0uZXhpdCgtMSk7CiAgICAgICAgfQogICAgfQoKICAgIGludCBuOwogICAgaW50IHByaW1lMSwgcHJpbWUyOwogICAgaW50IG1vZDEsIG1vZDI7CiAgICBpbnRbXSBwb3cxLCBwb3cyOwogICAgaW50W10gaDEsIGgyOwoKICAgIHZvaWQgaW5pdChjaGFyW10gcykgewogICAgICAgIFJhbmRvbSBybmQgPSBuZXcgUmFuZG9tKCk7CiAgICAgICAgcHJpbWUxID0gQmlnSW50ZWdlci52YWx1ZU9mKDMwMDAgKyBybmQubmV4dEludCg1MDApKQogICAgICAgICAgICAgICAgLm5leHRQcm9iYWJsZVByaW1lKCkuaW50VmFsdWUoKTsKICAgICAgICBwcmltZTIgPSBCaWdJbnRlZ2VyLnZhbHVlT2YoNDAwMCArIHJuZC5uZXh0SW50KDUwMCkpCiAgICAgICAgICAgICAgICAubmV4dFByb2JhYmxlUHJpbWUoKS5pbnRWYWx1ZSgpOwogICAgICAgIG1vZDEgPSBCaWdJbnRlZ2VyLnZhbHVlT2YoKGludCkgMWU5ICsgcm5kLm5leHRJbnQoNTAwKSkKICAgICAgICAgICAgICAgIC5uZXh0UHJvYmFibGVQcmltZSgpLmludFZhbHVlKCk7CiAgICAgICAgbW9kMiA9IEJpZ0ludGVnZXIudmFsdWVPZigoaW50KSAxZTkgKyAoaW50KSAxZTYgKyBybmQubmV4dEludCg1MDApKQogICAgICAgICAgICAgICAgLm5leHRQcm9iYWJsZVByaW1lKCkuaW50VmFsdWUoKTsKICAgICAgICBuID0gcy5sZW5ndGg7CiAgICAgICAgcG93MSA9IG5ldyBpbnRbbl07CiAgICAgICAgcG93MiA9IG5ldyBpbnRbbl07CiAgICAgICAgcG93MVswXSA9IHBvdzJbMF0gPSAxOwogICAgICAgIGZvciAoaW50IGkgPSAxOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgICAgIHBvdzFbaV0gPSAoaW50KSAoKGxvbmcpIHBvdzFbaSAtIDFdICogcHJpbWUxICUgbW9kMSk7CiAgICAgICAgICAgIHBvdzJbaV0gPSAoaW50KSAoKGxvbmcpIHBvdzJbaSAtIDFdICogcHJpbWUyICUgbW9kMik7CiAgICAgICAgfQogICAgICAgIGgxID0gbmV3IGludFtuXTsKICAgICAgICBoMiA9IG5ldyBpbnRbbl07CiAgICAgICAgaDFbMF0gPSBoMlswXSA9IHNbMF07CiAgICAgICAgZm9yIChpbnQgaSA9IDE7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgaDFbaV0gPSAoaW50KSAoKGgxW2kgLSAxXSArIChsb25nKSBwb3cxW2ldICogc1tpXSkgJSBtb2QxKTsKICAgICAgICAgICAgaDJbaV0gPSAoaW50KSAoKGgyW2kgLSAxXSArIChsb25nKSBwb3cyW2ldICogc1tpXSkgJSBtb2QyKTsKICAgICAgICB9CiAgICB9CgogICAgbG9uZyBnZXRIYXNoKGludCBMLCBpbnQgUikgewogICAgICAgIGludCBoYXNoMSA9IGdldEhhc2goTCwgUiwgaDEsIHBvdzEsIG1vZDEpOwogICAgICAgIGludCBoYXNoMiA9IGdldEhhc2goTCwgUiwgaDIsIHBvdzIsIG1vZDIpOwogICAgICAgIHJldHVybiAoKGxvbmcpIGhhc2gxIDw8IDMyKSB8IGhhc2gyOwogICAgfQoKICAgIGludCBnZXRIYXNoKGludCBMLCBpbnQgUiwgaW50W10gaCwgaW50W10gcG93LCBpbnQgbW9kKSB7CiAgICAgICAgaW50IGhhc2ggPSBoW1JdOwogICAgICAgIGlmIChMID4gMCkKICAgICAgICAgICAgaGFzaCAtPSBoW0wgLSAxXTsKICAgICAgICBpZiAoaGFzaCA8IDApCiAgICAgICAgICAgIGhhc2ggKz0gbW9kOwogICAgICAgIHJldHVybiAoaW50KSAoKGxvbmcpIGhhc2ggKiBwb3dbbiAtIFIgLSAxXSAlIG1vZCk7CiAgICB9CgogICAgYm9vbGVhbiBlcXVhbHMoaW50IEwxLCBpbnQgUjEsIGludCBMMiwgaW50IFIyKSB7CiAgICAgICAgbG9uZyBoYXNoMSA9IGdldEhhc2goTDEsIFIxKTsKICAgICAgICBsb25nIGhhc2gyID0gZ2V0SGFzaChMMiwgUjIpOwogICAgICAgIHJldHVybiBoYXNoMSA9PSBoYXNoMjsKICAgIH0KCiAgICB2b2lkIHNvbHZlKCkgdGhyb3dzIElPRXhjZXB0aW9uIHsKICAgICAgICBpbnQgbiA9IEludGVnZXIucGFyc2VJbnQoaW4ucmVhZExpbmUoKSk7CiAgICAgICAgU3RyaW5nIHMgPSBpbi5yZWFkTGluZSgpOwogICAgICAgIFN0cmluZyB0ID0gaW4ucmVhZExpbmUoKTsKICAgICAgICBTdHJpbmdCdWlsZGVyIHNiID0gbmV3IFN0cmluZ0J1aWxkZXIoKTsKICAgICAgICBzYi5hcHBlbmQocyk7CiAgICAgICAgc2IuYXBwZW5kKHQpOwogICAgICAgIHNiLmFwcGVuZCh0KTsKICAgICAgICBjaGFyW10gcmVzID0gc2IudG9TdHJpbmcoKS50b0NoYXJBcnJheSgpOwoKICAgICAgICBpbml0KHJlcyk7CgogICAgICAgIGZvciAoaW50IHNoaWZ0ID0gMDsgc2hpZnQgPCBuOyBzaGlmdCsrKSB7CiAgICAgICAgICAgIGlmIChlcXVhbHMoMCwgbiAtIDEsIG4gKyBzaGlmdCwgMiAqIG4gLSAxICsgc2hpZnQpKSB7CiAgICAgICAgICAgICAgICBvdXQucHJpbnRsbihzaGlmdCk7CiAgICAgICAgICAgICAgICByZXR1cm47CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgb3V0LnByaW50bG4oLTEpOwoKICAgIH0KCn0=