Provided is a compound or a salt thereof, which has an excellent JAK inhibitory action, and is useful as a prophylactic or therapeutic agent for autoimmune diseases (rheumatoid arthritis, psoriasis, inflammatory bowel disease, Sjogren's syndrome, Behcet's syndrome, multiple sclerosis, systemic lupus erythematosus, etc.), cancer (leukemia, uterine leiomyosarcoma, prostate cancer, multiple myeloma, cachexia, myelofibrosis, etc.) and the like. The present invention relates to a compound represented by the formula (I) wherein each symbol is as defined in the present specification, or a salt thereof.