[Java - C++ - C - C# - JavaScript] easy o(1) space
if you like it pls upvote\n\nJava\n\nclass Solution {\n        public int smallestRepunitDivByK(int K) {\n        if (K % 2 == 0 || K % 5 == 0) return -1;\n    
if you like it pls upvote\n\nJava\n```\nclass Solution {\n        public int smallestRepunitDivByK(int K) {\n        if (K % 2 == 0 || K % 5 == 0) return -1;\n        int r = 0;\n        for (int N = 1; N <= K; N++) {\n            r = (r * 10 + 1) % K;\n            if (r == 0) return N;\n        }\n        return -1;\n    }\n}\n```\n\nC++\n```\nclass Solution {\npublic:\n    int smallestRepunitDivByK(int K) {\n        if (K % 2 == 0 || K % 5 == 0) return -1;\n        int r = 0;\n        for (int N = 1; N <= K; N++) {\n            r = (r * 10 + 1) % K;\n            if (r == 0) return N;\n        }\n        return -1;\n    }\n};\n```\n\nC\n```\nint smallestRepunitDivByK(int K){\n\tif (K % 2 == 0 || K % 5 == 0) return -1;\n        int r = 0;\n        for (int N = 1; N <= K; N++) {\n            r = (r * 10 + 1) % K;\n            if (r == 0) return N;\n     }\n     return -1;\n}\n```\n\nC#\n```\npublic class Solution {\n    public int SmallestRepunitDivByK(int K) {\n        if (K % 2 == 0 || K % 5 == 0) return -1;\n        int r = 0;\n        for (int N = 1; N <= K; N++) {\n            r = (r * 10 + 1) % K;\n            if (r == 0) return N;\n        }\n        return -1;\n    }\n}\n```\n\nJavaScript\n```\nvar smallestRepunitDivByK = function(K) {\n    if (K % 2 == 0 || K % 5 == 0) return -1;\n        var r = 0;\n        for (var N = 1; N <= K; N++) {\n            r = (r * 10 + 1) % K;\n            if (r == 0) return N;\n        }\n        return -1;\n};\n```
['C', 'Java', 'JavaScript']