Let B' denote the inverse of B. Then B^5=I implies B'^5=I.
Rearrange A*B=B*A^2 into
A = B*A^2*B'
Then substitute the latter equation into itself to get
A = B^2*A^4*B'^2
Substitute three more times to get the series of equations
A = B^3*A^8*B'^3
A = B^4*A^16*B'^4
A = B^5*A^32*B'^5
The last one simplifies to I = A^31.