Prova a vedere questa soluzione:

http://www.cavazza.it/pcciechi/2008/05/html/chiave.htm