#if true
//#if false
#define UTAKA_DEBUG
#endif

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Text;

namespace UtakaApp
{
    public partial class Program
    {
        public const string ContestName = "abc141";
        public const string ProblemName = "E";

        public static void Main(string[] args)
        {
#if UTAKA_LOCAL
//#if false
            new TestCaseCheckerForAtCoder().TestProblems();
#else
            var console = new MyConsole();
            new Program().Solve(System.Console.In, console);
            console.Flush();
#endif
            var sw = new StreamWriter(Console.OpenStandardOutput()) {AutoFlush = false};
        }

        public void Solve(TextReader textReader, IConsole console)
        {
            var cin = new ConsoleInput(textReader);

            int N = cin.ReadInt;
            string S = cin.Read;

            long ans = 0;

            int left = 0;
            int right = N;

            var rh = new StringRollingHash(S);

            while (right - left > 1)
            {
                int mid = left + (right - left) / 2;

                if (IsOk(mid, rh, N))
                {
                    left = mid;
                }
                else
                {
                    right = mid;
                }
            }

            console.WriteLine(left.ToString());
        }

        public bool IsOk(int d, StringRollingHash rh, int N)
        {
//            var map = new Dictionary<long, int>();
            var map = new Dictionary<string, int>();

            for (int i = 0; i + d <= N; i++)
            {
//                var hashes = rh.GetHashes(i, i + d);
//                var hash = rh.GetHash(i, i + d, 0);
                var hash = rh.GetAllHashString(i, i + d);

                if (map.ContainsKey(hash))
                {
                    var dist = i - map[hash];

                    if (dist >= d)
                    {
                        return true;
                    }
                }
                else
                {
                    map.Add(hash, i);
                }
            }

            long a = long.MaxValue;

            return false;
        }
    }


    public class StringRollingHash
    {
        private static readonly long[] Mods = {999999893, 999999937};

        private static readonly long[] Bases = {1013, 2009};

        private readonly long[,] Hashes;
        private readonly long[,] Powers;
        private readonly string S;

        public StringRollingHash(string s)
        {
            int modCount = Mods.Length;

            S = s;
            Hashes = new long[modCount, s.Length + 1];
            Powers = new long[modCount, s.Length + 1];

            for (int i = 0; i < modCount; i++)
            {
                Powers[i, 0] = 1;
                for (int j = 0; j < s.Length; j++)
                {
                    Hashes[i, j + 1] = (Hashes[i, j] * Bases[i] + s[j]) % Mods[i];
                    Powers[i, j + 1] = Powers[i, j] * Bases[i] % Mods[i];
                }
            }
        }

        /// <summary>
        /// Mods[i]におけるS[l,r)のハッシュを取得
        /// </summary>
        private long GetHash(int l, int r, int i)
        {
            long res = (Hashes[i, r] - Hashes[i, l] * Powers[i, r - l]) % Mods[i];
            if (res < 0)
            {
                res += Mods[i];
            }

            return res;
        }

        public string GetAllHashString(int l, int r)
        {
            long[] a = GetHashes(l, r);
            return $"{a[0]}{a[1]}";
        }

        /// <summary>
        /// 全てのModsにおけるs[l,r)のハッシュを取得
        /// </summary>
        public long[] GetHashes(int l, int r)
        {
            List<long> hashList = new List<long>();

            for (int i = 0; i < Mods.Length; i++)
            {
                hashList.Add(GetHash(l, r, i));
            }

            return hashList.ToArray();
        }

        /// <summary>
        /// 全てのModsでS[l,r)が一致するか
        /// 2つのhashを比較する
        /// </summary>
        public bool Match(StringRollingHash other, int l1, int l2, int len)
        {
            // 範囲外
            if (S.Length < l1 + len || other.S.Length < l2 + len)
            {
                return false;
            }

            for (var i = 0; i < Mods.Length; i++)
            {
                if (GetHash(l1, l1 + len, i) != other.GetHash(l2, l2 + len, i))
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// 最長共通接頭辞の長さ
        /// this.S[l1:]とother.S[l2:]
        /// </summary>
        public int GetLcp(StringRollingHash other, int l1, int l2)
        {
            var len = Math.Min(S.Length - l1, other.S.Length - l2);

            int low = -1, high = len + 1;
            while (high - low > 1)
            {
                var mid = low + (high - low) / 2;

                if (!Match(other, l1, l2, mid))
                {
                    high = mid;
                }
                else
                {
                    low = mid;
                }
            }

            return low;
        }

        /// <summary>
        /// 最長共通接頭辞の長さ
        /// </summary>
        public int GetLcp(int l1, int l2)
        {
            return GetLcp(this, l1, l2);
        }

        /// <summary>
        /// this.S[l1:]がotherで始まっているか
        /// </summary>
        public bool BeginWith(StringRollingHash other, int l1)
        {
            return Match(other, l1, 0, other.S.Length);
        }
    }


    public partial class Program
    {
        public const int ModLarge = 1000000007;
    }

    public class ConsoleInput
    {
        private readonly TextReader _stream;
        private readonly char _separator = ' ';
        private readonly Queue<string> inputStream;

        public ConsoleInput(TextReader stream, char separator = ' ')
        {
            _separator = separator;
            _stream = stream;
            inputStream = new Queue<string>();
        }

        public string Read
        {
            get
            {
                if (inputStream.Count != 0)
                {
                    return inputStream.Dequeue();
                }

                var tmp = _stream.ReadLine().Split(_separator);
                for (var i = 0; i < tmp.Length; ++i)
                {
                    inputStream.Enqueue(tmp[i]);
                }

                return inputStream.Dequeue();
            }
        }

        public string ReadLine => _stream.ReadLine();
        public int ReadInt => int.Parse(Read);
        public long ReadLong => long.Parse(Read);
        public double ReadDouble => double.Parse(Read);

        public string[] ReadStrArray(long N)
        {
            var ret = new string[N];
            for (long i = 0; i < N; ++i)
            {
                ret[i] = Read;
            }

            return ret;
        }

        public int[] ReadIntArray(long N)
        {
            var ret = new int[N];
            for (long i = 0; i < N; ++i)
            {
                ret[i] = ReadInt;
            }

            return ret;
        }

        public long[] ReadLongArray(long N)
        {
            var ret = new long[N];
            for (long i = 0; i < N; ++i)
            {
                ret[i] = ReadLong;
            }

            return ret;
        }
    }

    public interface IConsole
    {
        void Flush();
        void Write(string str);
        void WriteLine(string str);
    }

    public class MyConsole : IConsole
    {
        public MyConsole()
        {
            var sw = new StreamWriter(Console.OpenStandardOutput()) {AutoFlush = false};
            Console.SetOut(sw);
        }

        public void Flush()
        {
            Console.Out.Flush();
        }

        public void Write(string str)
        {
            Console.Write(str);
        }

        public void WriteLine(string str)
        {
            Console.WriteLine(str);
        }
    }

#if UTAKA_LOCAL

    public class DebugConsole : IConsole
    {
        private readonly StringBuilder mSb;

        public DebugConsole()
        {
            mSb = new StringBuilder();
        }

        public void Flush()
        {
            // 何もしない
        }

        public void Write(string str)
        {
            mSb.Append(str);
        }

        public void WriteLine(string str)
        {
            mSb.AppendLine(str);
        }

        public string GetAllOutput()
        {
            return mSb.ToString();
        }
    }

#endif

#if UTAKA_LOCAL

    public class TestCaseCheckerForAtCoder
    {
        private string DirectoryPath => $"../../../../{Program.ContestName}/{Program.ProblemName}";

        private string GetInputFilePath(int testCaseNumber)
        {
            //return $"{DirectoryPath}/sample-{testCaseNumber}.in";
            return $"{DirectoryPath}/in_{testCaseNumber}.txt";
        }

        private string GetOutputFilePath(int testCaseNumber)
        {
            //return $"{DirectoryPath}/sample-{testCaseNumber}.out";
            return $"{DirectoryPath}/out_{testCaseNumber}.txt";
        }

        public bool TestProblem(int testCaseNumber)
        {
            var inputFilePath = GetInputFilePath(testCaseNumber);
            var outputFilePath = GetOutputFilePath(testCaseNumber);

            TextReader inputStream = new StreamReader(inputFilePath);

            var debugConsoleWriter = new DebugConsole();

            new Program().Solve(inputStream, debugConsoleWriter);

            var output = debugConsoleWriter.GetAllOutput();

            TextReader outputStream = new StreamReader(outputFilePath);
            var outputAnswer = outputStream.ReadToEnd();

            Dbg.WriteLine(output);
            Dbg.WriteLine(outputAnswer);

            var isCorrect = output == outputAnswer;

            return isCorrect;
        }

        public void TestProblems()
        {
            var isSuccessAll = true;

            var testCaseNumber = 1;
            while (true)
            {
                var inputFileName = GetInputFilePath(testCaseNumber);
                if (!File.Exists(inputFileName))
                {
                    break;
                }

                Console.WriteLine($"TestCase {testCaseNumber} =====================================================");

                var result = TestProblem(testCaseNumber);

                if (result)
                {
                    Console.WriteLine("Success");
                }
                else
                {
                    isSuccessAll = false;
                    Console.WriteLine("Failure *****");
                }

                testCaseNumber++;
            }

            if (isSuccessAll)
            {
                Console.WriteLine("!!!!!!!!! All Success !!!!!!!!!");
            }
        }
    }

#endif

#if UTAKA_LOCAL

    public static class Dbg
    {
        public static void WriteLine(string str)
        {
            Console.WriteLine(str);
        }

        public static void Write(string str)
        {
            Console.Write(str);
        }
    }
#else
    public static class Dbg
    {
        public static void WriteLine(string str)
        {
        }

        public static void Write(string str)
        {
        }
    }

#endif
}


Measured memory_bytes: 26836