The invention is concerned with novel nitrogen-containing heteroaryl compounds of formula (I) wherein A1, A2, R1, R2, R3, R4, R5 and R6 are as defined in the description and in the claims, as well as physiologically acceptable salts and esters thereof. These compounds inhibit PDE10A and can be used as therapeutics.