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;
int mod1;
int[] pow1;
int[] h1;
void init(char[] s) {
prime1
= BigInteger.
valueOf(3000 + rnd.
nextInt(500)) .nextProbablePrime().intValue();
mod1
= BigInteger.
valueOf((int) 1e9
+ rnd.
nextInt(500)) .nextProbablePrime().intValue();
n = s.length;
pow1 = new int[n];
pow1[0] = 1;
for (int i = 1; i < n; i++) {
pow1[i] = (int) ((long) pow1[i - 1] * prime1 % mod1);
}
h1 = new int[n];
h1[0] = s[0];
for (int i = 1; i < n; i++) {
h1[i] = (int) ((h1[i - 1] + (long) pow1[i] * s[i]) % mod1);
}
}
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, h1, pow1, mod1);
long hash2 = getHash(L2, R2, h1, pow1, mod1);
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);
}
}
aW1wb3J0IGphdmEuaW8uKjsKaW1wb3J0IGphdmEubWF0aC5CaWdJbnRlZ2VyOwppbXBvcnQgamF2YS51dGlsLio7CgpjbGFzcyBDeWNsaWNIYXNoZXMgaW1wbGVtZW50cyBSdW5uYWJsZSB7CgogICAgcHJpdmF0ZSBCdWZmZXJlZFJlYWRlciBpbjsKICAgIHByaXZhdGUgUHJpbnRXcml0ZXIgb3V0OwoKICAgIHZvaWQgaW5pdCgpIHRocm93cyBGaWxlTm90Rm91bmRFeGNlcHRpb24gewogICAgICAgIGluID0gbmV3IEJ1ZmZlcmVkUmVhZGVyKG5ldyBJbnB1dFN0cmVhbVJlYWRlcihTeXN0ZW0uaW4pKTsKICAgICAgICBvdXQgPSBuZXcgUHJpbnRXcml0ZXIoU3lzdGVtLm91dCk7CiAgICB9CgogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIG5ldyBUaHJlYWQobnVsbCwgbmV3IEN5Y2xpY0hhc2hlcygpLCAiIiwgMWwgKiAyMDAgKiAxMDI0ICogMTAyNCkuc3RhcnQoKTsKICAgIH0KCiAgICBwdWJsaWMgdm9pZCBydW4oKSB7CiAgICAgICAgdHJ5IHsKICAgICAgICAgICAgaW5pdCgpOwogICAgICAgICAgICBzb2x2ZSgpOwogICAgICAgICAgICBvdXQuY2xvc2UoKTsKICAgICAgICB9IGNhdGNoIChFeGNlcHRpb24gZSkgewogICAgICAgICAgICBlLnByaW50U3RhY2tUcmFjZSgpOwogICAgICAgICAgICBTeXN0ZW0uZXhpdCgtMSk7CiAgICAgICAgfQogICAgfQoKICAgIGludCBuOwogICAgaW50IHByaW1lMTsKICAgIGludCBtb2QxOwogICAgaW50W10gcG93MTsKICAgIGludFtdIGgxOwoKICAgIHZvaWQgaW5pdChjaGFyW10gcykgewogICAgICAgIFJhbmRvbSBybmQgPSBuZXcgUmFuZG9tKCk7CiAgICAgICAgcHJpbWUxID0gQmlnSW50ZWdlci52YWx1ZU9mKDMwMDAgKyBybmQubmV4dEludCg1MDApKQogICAgICAgICAgICAgICAgLm5leHRQcm9iYWJsZVByaW1lKCkuaW50VmFsdWUoKTsKICAgICAgICBtb2QxID0gQmlnSW50ZWdlci52YWx1ZU9mKChpbnQpIDFlOSArIHJuZC5uZXh0SW50KDUwMCkpCiAgICAgICAgICAgICAgICAubmV4dFByb2JhYmxlUHJpbWUoKS5pbnRWYWx1ZSgpOwogICAgICAgIG4gPSBzLmxlbmd0aDsKICAgICAgICBwb3cxID0gbmV3IGludFtuXTsKICAgICAgICBwb3cxWzBdID0gMTsKICAgICAgICBmb3IgKGludCBpID0gMTsgaSA8IG47IGkrKykgewogICAgICAgICAgICBwb3cxW2ldID0gKGludCkgKChsb25nKSBwb3cxW2kgLSAxXSAqIHByaW1lMSAlIG1vZDEpOwogICAgICAgIH0KICAgICAgICBoMSA9IG5ldyBpbnRbbl07CiAgICAgICAgaDFbMF0gPSBzWzBdOwogICAgICAgIGZvciAoaW50IGkgPSAxOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgICAgIGgxW2ldID0gKGludCkgKChoMVtpIC0gMV0gKyAobG9uZykgcG93MVtpXSAqIHNbaV0pICUgbW9kMSk7CiAgICAgICAgfQogICAgfQoKICAgIGludCBnZXRIYXNoKGludCBMLCBpbnQgUiwgaW50W10gaCwgaW50W10gcG93LCBpbnQgbW9kKSB7CiAgICAgICAgaW50IGhhc2ggPSBoW1JdOwogICAgICAgIGlmIChMID4gMCkKICAgICAgICAgICAgaGFzaCAtPSBoW0wgLSAxXTsKICAgICAgICBpZiAoaGFzaCA8IDApCiAgICAgICAgICAgIGhhc2ggKz0gbW9kOwogICAgICAgIHJldHVybiAoaW50KSAoKGxvbmcpIGhhc2ggKiBwb3dbbiAtIFIgLSAxXSAlIG1vZCk7CiAgICB9CgogICAgYm9vbGVhbiBlcXVhbHMoaW50IEwxLCBpbnQgUjEsIGludCBMMiwgaW50IFIyKSB7CiAgICAgICAgbG9uZyBoYXNoMSA9IGdldEhhc2goTDEsIFIxLCBoMSwgcG93MSwgbW9kMSk7CiAgICAgICAgbG9uZyBoYXNoMiA9IGdldEhhc2goTDIsIFIyLCBoMSwgcG93MSwgbW9kMSk7CiAgICAgICAgcmV0dXJuIGhhc2gxID09IGhhc2gyOwogICAgfQoKICAgIHZvaWQgc29sdmUoKSB0aHJvd3MgSU9FeGNlcHRpb24gewogICAgICAgIGludCBuID0gSW50ZWdlci5wYXJzZUludChpbi5yZWFkTGluZSgpKTsKICAgICAgICBTdHJpbmcgcyA9IGluLnJlYWRMaW5lKCk7CiAgICAgICAgU3RyaW5nIHQgPSBpbi5yZWFkTGluZSgpOwogICAgICAgIFN0cmluZ0J1aWxkZXIgc2IgPSBuZXcgU3RyaW5nQnVpbGRlcigpOwogICAgICAgIHNiLmFwcGVuZChzKTsKICAgICAgICBzYi5hcHBlbmQodCk7CiAgICAgICAgc2IuYXBwZW5kKHQpOwogICAgICAgIGNoYXJbXSByZXMgPSBzYi50b1N0cmluZygpLnRvQ2hhckFycmF5KCk7CgogICAgICAgIGluaXQocmVzKTsKCiAgICAgICAgZm9yIChpbnQgc2hpZnQgPSAwOyBzaGlmdCA8IG47IHNoaWZ0KyspIHsKICAgICAgICAgICAgaWYgKGVxdWFscygwLCBuIC0gMSwgbiArIHNoaWZ0LCAyICogbiAtIDEgKyBzaGlmdCkpIHsKICAgICAgICAgICAgICAgIG91dC5wcmludGxuKHNoaWZ0KTsKICAgICAgICAgICAgICAgIHJldHVybjsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBvdXQucHJpbnRsbigtMSk7CgogICAgfQoKfQ==