Understanding the eighteen-electron rule