#!/usr/bin/perl



$file = shift(@ARGV);
open (FILE, $file) or die "O ficheiro nao pode ser aberto: $!\n";

$NumberTokens = shift(@ARGV);

$freq=0;
$rank = $NumberTokens;

while ($line = <FILE>) {
      chomp($line);
    if ($rank > 0){
      ($freq, $type) = split (" ", $line) ;

      $Types{$type} = $rank;
    }
    $rank--;
}


$rank=$NumberTokens;
while ($line = <STDIN>) {
    if ($rank > 0){
      chomp($line);
      ($freq, $type) = split (" ", $line) ;

      if (defined $Types{$type}) {
          $valor += abs ($Types{$type} - $rank) ;
         #print STDERR "$valor\n" ;
	  
      }
      else {
         $valor += $rank ;
      }
     $Types2{$type} = rank;
   }
   
   $rank-- ;
   
}

$rank=$NumberTokens;
foreach $t (keys %Types) {
 if ($rank > 0){
    if (!defined $Types2{$t}) {
	$valor += $Types{$t} ;
       #print STDERR "$valor\n" ;
    }
 }
 $rank--
}

print "Distancia: ## $valor ##\n";
