p3 solved but performance is very bad
use a diff algorithm to improve performance