module mod_fenwick_tree
  implicit none
  type t_fenwick_tree
    private
    integer :: n, p
    integer, pointer :: arr(:) => null()
  contains
    procedure :: init => init_bit
    procedure :: release => release_bit
    procedure :: add => get_addition
    procedure :: sum => get_summation
    procedure :: range => get_partial_summation
    procedure :: val => get_value
    procedure :: lower_bound => get_lower_bound
    procedure :: update => get_updated
    procedure :: max => get_maximum
  end type t_fenwick_tree
contains
  subroutine init_bit(this,n)
    class(t_fenwick_tree), intent(inout) :: this
    integer, intent(in) :: n
    integer :: p
    p = 1
    this%n = n
    allocate(this%arr(n))
    this%arr = 0
    do while (lshift(p,1) <= n)
      p = lshift(p,1)
    end do
    this%p = p
  end
  subroutine release_bit(this)
    class(t_fenwick_tree), intent(inout) :: this
    if (associated(this%arr)) deallocate(this%arr)
  end
  subroutine get_addition(this,i,v)
    class(t_fenwick_tree), intent(inout) :: this
    integer, intent(in) :: i
    integer, intent(in) :: v
    integer :: x
    x = i
    do while (x <= this%n)
      this%arr(x) = this%arr(x)+v
      x = x+and(x,-x)
    end do
  end
  function get_summation(this,i) result(s)
    class(t_fenwick_tree), intent(in) :: this
    integer, intent(in) :: i
    integer :: x
    integer :: s
    s = -1
    if (i > this%n) return
    s = 0
    x = i
    do while (x > 0)
      s = s+this%arr(x)
      x = x-and(x,-x)
    end do
  end
  function get_partial_summation(this,l,r) result(s)
    class(t_fenwick_tree), intent(in) :: this
    integer, intent(in) :: l, r
    integer :: i, j
    integer :: s
    s = -1
    if (l > r) return
    s = 0
    i = l-1
    j = r
    do while (j > i)
      s = s+this%arr(j)
      j = j-and(j,-j)
    end do
    do while (i > j)
      s = s-this%arr(i)
      i = i-and(i,-i)
    end do
  end
  function get_value(this,i) result(v)
    class(t_fenwick_tree), intent(in) :: this
    integer, intent(in) :: i
    integer :: x
    integer :: v
    v = get_partial_summation(this,i,i)
  end
  function get_lower_bound(this,v) result(x)
    class(t_fenwick_tree), intent(in) :: this
    integer, intent(in) :: v
    integer :: x, k
    integer :: w
    x = 0
    if (v <= 0) return
    k = this%p
    w = v
    do while (k > 0)
      if (x+k <= this%n .and. this%arr(x+k) < w) then
        w = w-this%arr(x+k)
        x = x+k
      end if
      k = rshift(k,1)
    end do
    x = x+1
  end
  subroutine get_updated(this,i,v)
    class(t_fenwick_tree), intent(inout) :: this
    integer, intent(in) :: i
    integer, intent(in) :: v
    integer :: x
    x = i
    do while (x <= this%n)
      if (this%arr(x) < v) this%arr(x) = v
      x = x+and(x,-x)
    end do
  end
  function get_maximum(this,i) result(m)
    class(t_fenwick_tree), intent(in) :: this
    integer, intent(in) :: i
    integer :: x, m
    x = i
    m = 0
    do while (x > 0)
      m = max(m,this%arr(x))
      x = x-and(x,-x)
    end do
  end
end module mod_fenwick_tree
program fountain_walk
  implicit none
  real(8), parameter :: pi = 3.14159265358979d0
  real(8), parameter :: k1 = 100.d0, k2 = 20.d0-5.d0*pi, k3 = 10.d0*pi-20.d0
  integer :: n, i
  integer(8) :: x1, y1, x2, y2, x(200000) = 0_8, y(200000) = 0_8
  integer(8) :: s, p, q, k
  character(64) :: ans
  read(*,*) x1, y1, x2, y2
  read(*,*) n
  do i = 1, n
    read(*,*) x(i), y(i)
  end do
  if (x1 > x2) then
    call swap(x1,x2)
    call swap(y1,y2)
  end if
  if (y1 > y2) then
    y1 = -y1
    y2 = -y2
    y(1:n) = -y(1:n)
  end if
  s = x2-x1+y2-y1
  k = int(max(lis(x(1:n),y(1:n),x1,y1,x2,y2),lis(y(1:n),x(1:n),y1,x1,y2,x2)),8)
  if (k == min(x2-x1,y2-y1)+1_8) then
    p = k-1_8
    q = 1_8
  else
    p = k
    q = 0_8
  end if
  write(ans,'(f32.16)') k1*s-k2*p+k3*q
  write(*,'(a)') trim(adjustl(ans))
contains
  subroutine swap(a,b)
    integer(8), intent(inout) :: a, b
    a = xor(a,b)
    b = xor(a,b)
    a = xor(a,b)
  end
  integer function lis(x,y,x1,y1,x2,y2)
    use mod_fenwick_tree
    integer(8), intent(in) :: x(:), y(:), x1, y1, x2, y2
    integer(8) :: c(size(x),2)
    integer :: n, z(size(x)), t, i, j, k
    type(t_fenwick_tree) :: bit
    lis = 0
    n = size(x)
    c(:,1) = x
    c(:,2) = y
    call merge_sort(c)
    j = 1
    do while (j <= n .and. c(j,1) < x1)
      j = j+1
    end do
    if (j > n) return
    k = n
    do while (k >= 1 .and. c(k,1) > x2)
      k = k-1
    end do
    if (k < 1) return
    z = compress(c(:,2))
    call bit%init(n)
    do i = j, k
      if (c(i,2) < y1 .or. y2 < c(i,2)) cycle
      t = bit%max(z(i)-1)+1
      lis = max(lis,t)
      call bit%update(z(i),t)
    end do
    call bit%release()
  end
  function compress(x) result(ret)
    implicit none
    integer(8), intent(in) :: x(:)
    integer(8) :: y(size(x),2)
    integer :: n, i, k, ret(size(x))
    n = size(x)
    y(:,1) = x
    do i = 1, n
      y(i,2) = int(i,8)
    end do
    call merge_sort(y)
    k = 0
    ret(y(1,2)) = 1
    do i = 2, n
      if (y(i,1) == y(i-1,1)) k = k+1
      ret(y(i,2)) = i-k
    end do
  end
  subroutine merge_sort(c)
    integer(8), intent(inout) :: c(:,:)
    integer :: n, m, l, u, i
    n = size(c(:,1))
    m = n
    l = 1
    do while (m > 1)
      do i = 1, m/2
        u = min(2*i*l,n)
        call merger(c(2*(i-1)*l+1:(2*i-1)*l,:),c((2*i-1)*l+1:u,:))
      end do
      l = 2*l
      m = (m+1)/2
    end do
  end
  subroutine merger(c1,c2)
    integer(8), intent(inout) :: c1(:,:), c2(:,:)
    integer(8) :: c(size(c1(:,1))+size(c2(:,1)),size(c2(1,:)))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(c1(:,1))
    n2 = size(c2(:,1))
    do while (i1 <= n1 .and. i2 <= n2)
      if (c1(i1,1) < c2(i2,1) .or. &
      & c1(i1,1) == c2(i2,1) .and. c1(i1,2) > c2(i2,2)) 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 <= n1) then
      c(i1+i2-1:n1+n2,:) = c1(i1:n1,:)
    else if (i2 <= n2) then
      c(i1+i2-1:n1+n2,:) = c2(i2:n2,:)
    end if
    c1 = c(1:n1,:)
    c2 = c(n1+1:n1+n2,:)
  end
end program fountain_walk

Measured memory_bytes: 13496