use std::{collections::BinaryHeap, cmp::Reverse}; const INF: usize = 1usize << 60; const OFFSET: usize = 10000001; fn main() { let mut t = String::new(); std::io::stdin().read_line(&mut t).ok(); let t: isize = t.trim().parse().unwrap(); let mut xa = String::new(); std::io::stdin().read_line(&mut xa).ok(); let xa: Vec = xa.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let x = xa[0]; let a = xa[1]; let mut yb = String::new(); std::io::stdin().read_line(&mut yb).ok(); let yb: Vec = yb.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let y = yb[0]; let b = yb[1]; let (t, start_cost) = if t < 0 { let temp = t.abs() as usize / b + if t / b as isize == 0 { 0 } else { 1 }; let s = (b * temp) as isize + t; (s, temp * y) } else { let temp = t as usize / a; let s = t - (a * temp) as isize; (s, temp * x) }; let t = (t + OFFSET as isize) as usize; let goal = t as usize; let mut checked = vec![INF; OFFSET*2+1]; checked[OFFSET] = start_cost; let mut pque = BinaryHeap::new(); pque.push((Reverse(start_cost), OFFSET)); while let Some((Reverse(cost), pos)) = pque.pop() { if checked[goal] <= cost { break; } if checked[pos] < cost { continue; } if pos+1 < checked.len() && pos+1 <= goal && checked[pos+1] > cost + 1 { pque.push((Reverse(cost+1), pos+1)); checked[pos+1] = cost+1; } if pos+a < checked.len() && pos < goal && checked[pos+a] > cost + x { pque.push((Reverse(cost+x), pos+a)); checked[pos+a] = cost+x; } if pos >= b && pos > goal && checked[pos-b] > cost + y { pque.push((Reverse(cost+y), pos-b)); checked[pos-b] = cost+y; } } println!("{}", checked[goal]); }