Compounds of formula I or pharmaceutically acceptable salts thereof, wherein R5 is a group of formula (a) or (b): and wherein m, n, p, q, X, Y, R1, R2, R3, R4, R5, R6, R7, R8 and R9 are as defined herein. The compounds and compositions disclosed herein are useful to modulate the activity of JNK and treat diseases associated with JNK activity.