m, n, m0, md, n0, nd = gets.split.map(&:to_i)

degs_l = [m0]
(m - 1).times do
    degs_l << (degs_l[-1] * 58 + md) % (n + 1)
end

degs_r = [n0]
(n - 1).times do
    degs_r << (degs_r[-1] * 58 + nd) % (m + 1)
end

p [degs_l.inject(:+), degs_r.inject(:+)].min