N=gets.hex t={} $<.map{|s|eval"t[0d%d%d]=%d"%s.split} p (R=*0...N).permutation.map{|a|R.map{|i|(i...N).map{|j|t[a[i]*10+a[j]]||0}.sum}.sum}.max