program synthetic_kadomatsu
  implicit none
  integer :: n, a, b, c, l(8), i, p, t, m, j, k, idx, jdx
  read(*,*) n, a, b, c
  l = 0
  t = 0
  do i = 1, n
    read(*,*) l(i)
    if (a.eq.l(i)) then
      a = 0
      l(i) = 0
      t = t + 1
    else if (b.eq.l(i)) then
      b = 0
      l(i) = 0
      t = t + 1
    else if (c.eq.l(i)) then
      c = 0
      l(i) = 0
      t = t + 1
    end if
  end do
  call merge_sort(8,l)
  n = n - t
  p = 0
  if (c.ne.0) then
    if (c.lt.l(n)) then
      p = p + abs(l(n)-c)
      l(n) = 0
      n = n - 1
    else
      m = 1000
      do i = 1, n
        if (c.lt.l(i)) k = i
        if (m.gt.abs(c-l(i))) then
          m = abs(c-l(i))
          idx = i
        end if
      end do
      if (m.gt.10) then
        jdx = -1
        do i = k, n-1
          do j = i+1, n
            if (abs(l(i)+l(j)-c)+10.lt.m) then
              m = abs(l(i)+l(j)-c)+10
              idx = i
              jdx = j
            end if
          end do
        end do
        p = p + m
        l(idx:7) = l(idx+1:8)
        n = n - 1
        if (jdx > 0) then
          l(jdx:7) = l(jdx+1:8)
          n = n - 1
        end if
      end if
    end if
  end if
  if (b.ne.0) then
    if (b.lt.l(n)) then
      p = p + abs(l(n)-b)
      l(n) = 0
      n = n - 1
    else
      m = 1000
      do i = 1, n
        if (b.lt.l(i)) k = i
        if (m.gt.abs(b-l(i))) then
          m = abs(b-l(i))
          idx = i
        end if
      end do
      if (m.gt.10) then
        jdx = -1
        do i = k, n-1
          do j = i+1, n
            if (abs(l(i)+l(j)-b)+10.lt.m) then
              m = abs(l(i)+l(j)-b)+10
              idx = i
              jdx = j
            end if
          end do
        end do
        p = p + m
        l(idx:7) = l(idx+1:8)
        n = n - 1
        if (jdx > 0) then
          l(jdx:7) = l(jdx+1:8)
          n = n - 1
        end if
      end if
    end if
  end if
  if (a.ne.0) then
    if (a.lt.l(n)) then
      p = p + abs(l(n)-a)
      l(n) = 0
      n = n - 1
    else
      m = 1000
      do i = 1, n
        if (a.lt.l(i)) k = i
        if (m.gt.abs(a-l(i))) then
          m = abs(a-l(i))
          idx = i
        end if
      end do
      if (m.gt.10) then
        jdx = -1
        do i = k, n-1
          do j = i+1, n
            if (abs(l(i)+l(j)-a)+10.lt.m) then
              m = abs(l(i)+l(j)-a)+10
              idx = i
              jdx = j
            end if
          end do
        end do
        p = p + m
        l(idx:7) = l(idx+1:8)
        n = n - 1
        if (jdx > 0) then
          l(jdx:7) = l(jdx+1:8)
          n = n - 1
        end if
      end if
    end if
  end if
  write(*,'(i0)') p
  stop
contains
  subroutine merge_sort(n,c)
    implicit none
    integer, intent(in)    :: n
    integer, intent(inout) :: c(n)
    integer                :: number, length, i, ubound
    number = n
    length = 1
    do while (number .gt. 1)
       do i = 1, number/2
          ubound = min(2*i*length,n)
          call merge(c(2*(i-1)*length+1:(2*i-1)*length),c((2*i-1)*length+1:ubound))
       end do
       length = 2*length
       number = (number+1)/2
    end do
    return
  end subroutine merge_sort

  subroutine merge(c1,c2)
    implicit none
    integer, intent(inout) :: c1(:), c2(:)
    integer                :: c(size(c1)+size(c2))
    integer                :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(c1)
    n2 = size(c2)
    do while (i1 .le. n1 .and. i2 .le. n2)
       if (c1(i1) .ge. c2(i2)) then
          c(i1+i2-1) = c1(i1)
          i1 = i1+1
       else
          c(i1+i2-1) = c2(i2)
          i2 = i2+1
       end if
    end do
    if (i1 .le. n1) then
       c(i1+i2-1:n1+n2) = c1(i1:n1)
    else if (i2 .le. n2) then
       c(i1+i2-1:n1+n2) = c2(i2:n2)
    end if
    c1 = c(1:n1)
    c2 = c(n1+1:n1+n2)
    return
  end subroutine merge
end program synthetic_kadomatsu
