sub f{map{push@{-$_},"$a[--$|] $'"for@a=/\d+ /g}<>;my$r=1;$r+=$t=!$c[$_]++&&&f,$x+=/ /*$'*$t*($N-$t)<<1for@{-$_};$r}f for$N=<>;print$x