Nitrogen is in group 15 (or 5A), meaning it has 5 valence electrons and needs to gain 3 electrons to complete its octet, making it likely to form a -3 ion (N³⁻). Oxygen, in group 16 (6A), typically gains 2 electrons to form a -2 ion (O²⁻). Magnesium and Sodium, being metals, tend to lose electrons and form positive ions (Mg²⁺ and Na⁺ respectively).