factor out counting distinct solutions