Millions at risk if Scottish councils fail to retain teacher