program three_substrings
  integer, parameter :: u = 2000
  character(u) :: a, b, c
  integer :: l, m, n, x = 3*u, i, j
  logical, dimension(-5*u:5*u) :: ab = .false., ac = .false., bc = .false.
  read(*,*) a
  read(*,*) b
  read(*,*) c
  l = len_trim(a)
  m = len_trim(b)
  n = len_trim(c)
  do i = 0, l-1
    do j = 0, m-1
      if (.not.match(a(i+1:i+1),b(j+1:j+1))) ab(i-j) = .true.
    end do
  end do
  do i = 0, l-1
    do j = 0, n-1
      if (.not.match(a(i+1:i+1),c(j+1:j+1))) ac(i-j) = .true.
    end do
  end do
  do i = 0, m-1
    do j = 0, n-1
      if (.not.match(b(i+1:i+1),c(j+1:j+1))) bc(i-j) = .true.
    end do
  end do
  do i = -2*u, 2*u
    do j = -2*u, 2*u
      if (ab(i) .or. ac(j) .or. bc(j-i)) cycle
      x = min(x,max(l,m+i,n+j)-min(i,j,0))
    end do
  end do
  write(*,'(i0)') x
contains
  logical function match(s,t)
    character(1), intent(in) :: s, t
    match = s == "?" .or. t == "?" .or. s == t
  end
end program three_substrings