using System;

namespace ConsoleApp7
{
    class Program
    {
        public static String[] RemoveIndices(String[] IndicesArray, int RemoveAt)
        {
            String[] newIndicesArray = new String[IndicesArray.Length - 1];

            int i = 0;
            int j = 0;
            while (i < IndicesArray.Length)
            {
                if (i != RemoveAt)
                {
                    newIndicesArray[j] = IndicesArray[i];
                    j++;
                }

                i++;
            }

            return newIndicesArray;
        }
        static void Main(string[] args)
        {
            String s = Convert.ToString(Console.ReadLine());
            String u = Convert.ToString(Console.ReadLine());
            String[] o = new string[2];
            o = s.Split();
            int brnap = Convert.ToInt32(o[0]);
            String[] l = new String[brnap];
            l = u.Split();
            for (int i = 0; i < Convert.ToInt32(o[1])-1; i++)
            {
                int a = Convert.ToInt32(l[0]);
                int b = Convert.ToInt32(l.Length-1);
                if (a > b)
                {
                    l[0] = Convert.ToString(a - b);
                    RemoveIndices(l, l.Length - 1);
                }
                else
                {
                    l[l.Length - 1] = Convert.ToString(b-a);
                    RemoveIndices(l, 0);

                }
            }
            Console.WriteLine(l[0]);
        }
    }
}