<>=~s/./$n="$n$&"%6/ger; <>=~/.$/; print+(4,2,8,5,7,1)[$n**($&|2)%6];