program squirrel_merchant
  implicit none
  integer(8) :: n, a(3), b(3)
  read(*,*) n
  read(*,*) a(:)
  read(*,*) b(:)
  call exchange(n,a,b)
  call exchange(n,b,a)
  write(*,'(i0)') n
  stop
contains
  subroutine exchange(n,x,y)
    implicit none
    integer(8), intent(inout) :: n, x(3), y(3)
    integer(8) :: dp(0_8:n), i
    dp = 0_8
    do i = 0_8, n
      dp(i) = i
      if (i.ge.x(1)) dp(i) = max(dp(i),dp(i-x(1))+y(1))
      if (i.ge.x(2)) dp(i) = max(dp(i),dp(i-x(2))+y(2))
      if (i.ge.x(3)) dp(i) = max(dp(i),dp(i-x(3))+y(3))
    end do
    n = maxval(dp)
    return
  end subroutine exchange
end program squirrel_merchant