washing machine overflowing

my washing machine works other than when it is filling up in the beginning, or when it fills again at the start of a new cylce, it will overflow with water:( Any ideas?