int main() {
int k;
cin >> k;
int x1, y1;
cin >> x1 >> y1;
int x2, y2;
cin >> x2 >> y2;
int pos = 0;
while (k > 0) {
int jump = k; // 用目前的生命值決定跳躍距離
pos += jump; // 跳躍
int damage = 0;
if (pos % x1 == 0) damage += y1;
if (pos % x2 == 0) damage += y2;
k -= damage; // 扣血
}
cout << pos << endl;
return 0;
}