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