First-principles characterization of ferromagnetism in N-doped SrTiO3 and BaTiO3