The invention is concerned with novel nitrogen-containing heteroaryl derivatives of formula (I) wherein R1, R2, R3, R4, R5, A1, A2, and Y 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 medicaments.