import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;

int solve (int n, int[] a, int[] d) {
  debug stderr.writeln (a, ' ', d);
  int l = a.length.to!int;
  auto r = iota (0, l).array;
  int res = 0;
  do {
    int y = n;
    int t = n;
    foreach (i; 0 .. l) {
      int j = r[i];
      int k = t / a[j];
      t -= k * a[j];
      y += d[j] * k;
    }
    res = max (res, y);
  } while (nextPermutation (r));
  return res;
}

void main() {
  int n;
  int[3] a;
  int[3] b;
  readf (" %d", &n);
  foreach (t; 0 .. 3) {
    readf (" %d", &a[t]);
  }
  foreach (t; 0 .. 3) {
    readf (" %d", &b[t]);
  }

  int[] u, v;
  foreach (t; 0 .. 3) {
    if (a[t] < b[t]) {
      v ~= abs (a[t] - b[t]);
      u ~= a[t];
    }
  }
  n = solve (n, u, v);
  u = [];
  v = [];
  foreach (t; 0 .. 3) {
    if (a[t] > b[t]) {
      v ~= abs (a[t] - b[t]);
      u ~= b[t];
    }
  }
  writeln (solve (n, u, v));

