def ii() gets.chomp.to_i end def li() gets.chomp.split.map(&:to_i) end a,b,c,d,m=li() ans=0 a.upto(b) do |x| c.upto(d) do |y| v=(x+y)%m ans=v if ans