Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REP1(i, n) for (int i = 1, i##_len = (n); i < i##_len; ++i)
#define ll long long
static const int MAX = 10000;
static const ll INFTY = 1e12;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll MRINF = 5000000000000000;

int main() {

  ll K;
  cin >> K;
  ll n = 7;
  ll mod = n;
  ll count = 1;

  ll roop = n % K;
  if (K % 2 == 0 || K % 5 == 0) {
    cout << -1 << endl;
    return 0;
  }

  while (1) {
    mod = mod % K;
    if (mod == 0) {
      cout << count << endl;
      return 0;
    }

    if (count != 1) {
      if (mod == roop) {
        cout << -1 << endl;
        return 0;
      }
    }

    count++;
    mod = mod * 10 + 7;
  }
}
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\\  =  /O
               ____/`---'\\____
             .'  \\\\|     |//  `.
            /  \\\\|||  :  |||//  \\
           /  _||||| -:- |||||-  \\
           |   | \\\\\\  -  /// |   |
           | \\_|  ''\\---/''  |   |
           \\  .-\\__  `-`  ___/-. /
         ___`. .'  /--.--\\  `. . __
      ."" '<  `.___\\_<|>_/___.'  >'"".
     | | :  `- \\`.;`\\ _ /`;.`/ - ` : | |
     \\  \\ `-.   \\_ __\\ /__ _/   .-` /  /
======`-.____`-.___\\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         pass System Test!
*/

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REP1(i, n) for (int i = 1, i##_len = (n); i < i##_len; ++i)
#define ll long long
static const int MAX = 10000;
static const ll INFTY = 1e12;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll MRINF = 5000000000000000;

int main() {

  ll K;
  cin >> K;
  ll n = 7;
  ll mod = n;
  ll count = 1;

  ll roop = n % K;

  while (1) {
    mod = mod % K;
    if (mod == 0) {
      cout << count << endl;
      return 0;
    }
    if (count != 1) {
      if (mod == roop) {
        cout << -1 << endl;
        return 0;
      }
    }

    count++;
    mod = mod * 10 + 7;
  }
}
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\\  =  /O
               ____/`---'\\____
             .'  \\\\|     |//  `.
            /  \\\\|||  :  |||//  \\
           /  _||||| -:- |||||-  \\
           |   | \\\\\\  -  /// |   |
           | \\_|  ''\\---/''  |   |
           \\  .-\\__  `-`  ___/-. /
         ___`. .'  /--.--\\  `. . __
      ."" '<  `.___\\_<|>_/___.'  >'"".
     | | :  `- \\`.;`\\ _ /`;.`/ - ` : | |
     \\  \\ `-.   \\_ __\\ /__ _/   .-` /  /
======`-.____`-.___\\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         pass System Test!
*/