Phase-transition mechanism of h-BN → w-BN from first principles