Zero to π transition in superconductor-ferromagnet-superconductor junctions