using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); } static void Solve() { var p = NList; var ans = 0; for (var i = p[0]; i <= p[1]; ++i) for (var j = p[2]; j <= p[3]; ++j) ans = Math.Max(ans, (i + j) % p[4]); WriteLine(ans); } }