#include using namespace std; struct automat { vector> edges; vector link; vector length; vector koniec; int last; automat(string s) { edges.push_back({}); link.push_back(-1); length.push_back(0); last = 0; for (int i = 0; i < (int)s.size(); i++) { edges.push_back({}); length.push_back(i + 1); link.push_back(0); int r = edges.size() - 1; int p = last; while (p >= 0 && edges[p].find(s[i]) == edges[p].end()) { edges[p][s[i]]= r; p = link[p]; } if (p != -1) { int q = edges[p][s[i]]; if (length[p] + 1 == length[q]) { link[r] = q; } else { edges.push_back(edges[q]); length.push_back(length[p] + 1); link.push_back(link[q]); int qq = edges.size() - 1; link[q] = qq; link[r] = qq; while (p >= 0 && edges[p][s[i]]== q) { edges[p][s[i]] = qq; p = link[p]; } } } last = r; } koniec.resize(link.size(), false); int x = last; while (x) { koniec[x] = true; x = link[x]; } } }; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; string s; cin >> n >> s; automat a(s); int res = n - 1; int gdzie = 0; for (int i = n - 1; i >= 0; i--) { char c = s[i]; gdzie = a.edges[gdzie][c]; if (gdzie == 0) { break; } if (a.koniec[gdzie]) { res = i; } } cout << res << endl; return 0; }