class SegmentTree
	def initialize(arr, l = 0, r = arr.size - 1)
		@l = l
		@r = r
		if l == r
			@val = arr[l]
		else
			c = (l + r) / 2
			@left_child = self.class.new(arr, l, c)
			@right_child = self.class.new(arr, c+1, r)
		end
		@min = arr[l]
		@max = arr[r]
	end

	def query(i)
		force if @lazy
		return @val if @l == @r
		if i <= @left_child.r
			return @left_child.query(i)
		else
			return @right_child.query(i)
		end
	end

	def min_index(val)
		force if @lazy
		if @l == @r
			if @val == val
				return @l
			else
				return nil
			end
		end

		if val <= @left_child.max 
			return @left_child.min_index(val)
		else
			return @right_child.min_index(val)
		end
	end

	def force
		if @l == @r
			@val -= @lazy
			@val = 0 if @val < 0
		else
			@left_child.update
			@right_child.update
		end
		@lazy = nil
	end

	def update(l = @l, r = @r)
		force if @lazy
		if @l == l && @r == r
			@min -= 1
			@min = 0 if @min < 0
			@max -= 1
			@max = 0 if @max < 0
			@lazy = 1
		elsif r <= @left_child.r 
			@left_child.update(l, r)
			@min = @left_child.min
		elsif @right_child.l <= l
			@right_child.update(l, r)
			@max = @right_child.max
		else
			@left_child.update(l, @left_child.r)
			@min = @left_child.min
			@right_child.update(@right_child.l, r)
			@max = @right_child.max
		end
	end

	attr_reader :val, :min, :max, :l, :r
end

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

degs_l.sort!
degs_r.sort!

tree = SegmentTree.new(degs_r)
k = 0
degs_l.each do |x|
	next if x == 0
	y = tree.query(n - x)
	if y == 0
		i = tree.min_index(1)
		if i
			tree.update(i, n-1)
			k += n - i
		end
	else
		i = tree.min_index(y)
		tree.update(i, i+x-1)
		k += x
	end
end
p k