How to reduce overly-redundant for loops

visit shbcf.ru