!  CROSS
!  ROADS
!-------
! DANGER
	Program Rebus

	logical used(0:9)
      integer iv(0:9)
	
	character*1 c1,c2,c3
	character*26 letters
	character*10 word1,word2,answer

	word1 = '      SEND'
	word2 = '      MORE'
	answer ='     MONEY'
	letters=' '

	lword1 = len(word1)
	lword2 = len(word2)
	lanswer = len(answer)
	do l=1,10
	   c1 = word1(l:l)
	   if(c1.ne.' '.and.index(letters,c1).eq.0) letters = c1//letters
	   if(c1.eq.' ') lword1 = 10-l
	   c2 = word2(l:l)
	   if(c2.ne.' '.and.index(letters,c2).eq.0) letters = c2//letters
	   if(c2.eq.' ') lword2 = 10-l
	   c3 = answer(l:l)
	   if(c3.ne.' '.and.index(letters,c3).eq.0) letters = c3//letters
	   if(c3.eq.' ') lanswer = 10-l
	end do

	write(6,*) 'Word lengths  : ',lword1,lword2,lanswer
	write(6,*) 'Unique letters: '//letters

	nletters = index(letters,' ')-1

	if(nletters.gt.10) stop 'Too many unique letters!'

      do 1 iv0=0,9
          iv(0) = iv0
          do j=0,9
              used(j) = .false.
          end do
          used(iv(0)) = .true.         
          if(nletters.le.1) then
			call check(iv,letters,word1,word2,answer)
			used(iv(0)) = .false.
			goto 1
		endif
      do 2 iv1=0,9
          iv(1) = iv1
          if(used(iv(1))) cycle
          used(iv(1)) = .true.
          if(nletters.le.2) then
			call check(iv,letters,word1,word2,answer)
			used(iv(1)) = .false.
			goto 2
		endif
      do 3 iv2=0,9
          iv(2) = iv2
          if(used(iv(2))) cycle
          used(iv(2)) = .true.
          if(nletters.le.3) then
			call check(iv,letters,word1,word2,answer)
			used(iv(2)) = .false.
			goto 3
		endif
      do 4 iv3=0,9
          iv(3) = iv3
          if(used(iv(3))) cycle
          used(iv(3)) = .true.
          if(nletters.le.4) then
			call check(iv,letters,word1,word2,answer)
			used(iv(3)) = .false.
			goto 4
		endif
      do 5 iv4=0,9
          iv(4) = iv4
          if(used(iv(4))) cycle
          used(iv(4)) = .true.
          if(nletters.le.5) then
			call check(iv,letters,word1,word2,answer)
			used(iv(4)) = .false.
			goto 5
		endif
      do 6 iv5=0,9
          iv(5) = iv5
          if(used(iv(5))) cycle
          used(iv(5)) = .true.
          if(nletters.le.6) then
			call check(iv,letters,word1,word2,answer)
			used(iv(5)) = .false.
			goto 6
		endif
      do 7 iv6=0,9
          iv(6) = iv6
          if(used(iv(6))) cycle
          used(iv(6)) = .true.
          if(nletters.le.7) then
			call check(iv,letters,word1,word2,answer)
			used(iv(6)) = .false.
			goto 7
		endif
      do 8 iv7=0,9
          iv(7) = iv7
          if(used(iv(7))) cycle
          used(iv(7)) = .true.
          if(nletters.le.8) then
			call check(iv,letters,word1,word2,answer)
			used(iv(7)) = .false.
			goto 8
		endif
      do 9 iv8=0,9
          iv(8) = iv8
          if(used(iv(8))) cycle
          used(iv(8)) = .true.
          if(nletters.le.9) then
			call check(iv,letters,word1,word2,answer)
			used(iv(8)) = .false.
			goto 9
		endif
      do 10 iv9=0,9
          iv(9) = iv9
          if(used(iv(9))) cycle
          used(iv(9)) = .true.
          call check(iv,letters,word1,word2,answer)
          used(iv(9)) = .false.          
   10 continue
      used(iv(8)) = .false.
    9 continue
      used(iv(7)) = .false.
    8 continue
      used(iv(6)) = .false.
    7 continue
      used(iv(5)) = .false.
    6 continue
      used(iv(4)) = .false.
    5 continue
      used(iv(3)) = .false.
    4 continue
      used(iv(2)) = .false.
    3 continue
      used(iv(1)) = .false.
    2 continue
      used(iv(0)) = .false.
    1 continue
	End

      Subroutine check(iv,letters,word1,word2,answer)
      integer iv(0:9)
      character*(*) letters,word1,word2,answer
	character*(10) cword1,cword2,canswer
      character*1 c1,c2,c3
	logical f1,f2,f3

          iword1 = 0
          iword2 = 0
          ianswer = 0
	cword1 = ' '
	cword2 = ' '
	canswer = ' '
	f1 = .false.
	f2 = .false.
	f3 = .false.

          do l=1,10
              c1 = word1(l:l)
              if(c1.ne.' ') then
				if(.not.f1.and.j.eq.0) return
                  j = iv(index(letters,c1)-1)				
				if(.not.f1.and.j.eq.0) return
				f1 = .true.
                  iword1 = iword1 + j*10**(10-l)
				write(cword1(l:l),'(i1)') j
              endif
                  
              c2 = word2(l:l)
              if(c2.ne.' ') then
                  j = iv(index(letters,c2)-1)
				if(.not.f2.and.j.eq.0) return
				f2 = .true.
                  iword2 = iword2 + j*10**(10-l)
				write(cword2(l:l),'(i1)') j
              endif
              c3 = answer(l:l)
              if(c3.ne.' ') then
                  j = iv(index(letters,c3)-1)
				if(.not.f3.and.j.eq.0) return
				f3 = .true.
                  ianswer = ianswer + j*10**(10-l)
				write(canswer(l:l),'(i1)') j
              endif
          end do
          if(iword1+iword2.eq.ianswer) then
              write(6,'(10i1)') iv
			write(6,*) word1,cword1
			write(6,*) word2,cword2
			write(6,*) answer,canswer
          endif
      End
