using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Carobnjak
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split();
            int n = int.Parse(s[0]);
            int k = int.Parse(s[1]);
            List<int> a = new List<int>();
            string[] st = Console.ReadLine().Split();
            for (int i = 0; i < n; i++)
            {
                a.Add(int.Parse(st[i]));
            }
            while (a.Count() > 1)
            {
                if (a[a.Count() - 1] > a[0])
                {
                    a[a.Count() - 1] -= a[0];
                    a.RemoveAt(0);
                }
                else
                {
                    a[0] -= a[a.Count() - 1];
                    a.RemoveAt(a.Count() - 1);
                }
            }
            Console.WriteLine(a[0]);
        }
    }
}