module ch
  implicit none
  type char
     character(10)      :: chara
  end type char
  type(char),allocatable :: namec(:)
  type(char),allocatable :: namecmax(:)
end module ch
program main
   
  use ch, only : namec, namecmax

  implicit none
  integer :: n
  integer, allocatable :: s(:)
  integer :: i,j,k, maxvals
  
  read(*,*) n
  allocate( s(n) )
  allocate( namec(n) )
  do i = 1, n
    read(*,*) namec(i)%chara
  end do

  call quicksort1( 1, n ) 

  j = 0 
  do i = 1, n
    if( namec(i)%chara /= namec(i+1)%chara ) then
      j = j + 1
    end if 
  end do

  print*,j

end program main
recursive subroutine quicksort1( first, last )

  use ch, only : namec, namecmax

  implicit none
  integer,intent(in)::first,last
  !
  ! Original
  ! https://gist.github.com/t-nissie/479f0f16966925fa29ea
  !
  character(100)::x,t
  integer::i,j

  x = namec((first+last)/2)%chara
  i = first
  j = last
  do
     do while ( LGT( x, namec(i)%chara ) )
        i=i+1
     end do
     do while ( LGT( namec(j)%chara, x ) )
        j=j-1
     end do
     if (i >= j) exit
     t = namec(i)%chara
     namec(i)%chara = namec(j)%chara
     namec(j)%chara = t
     i=i+1
     j=j-1
  end do
  if (first < i-1) call quicksort1( first, i-1  )
  if (j+1 < last)  call quicksort1( j+1  , last )

  return

end subroutine quicksort1
