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

/*
                                             ::                     ::
                                             ':                     :
                                              :.                    :
                                           ;' ::                   ::  '
                                          .'  ';                   ;'  '.
                                         ::    :;                 ;:    ::
                                         ;      :;.             ,;:     ::
                                         :;      :;:           ,;"      ::
                                         ::.      ':;  ..,.;  ;:'     ,.;:
                                          "'"...   '::,::::: ;:   .;.;""'
                                              '"""....;:::::;,;.;"""
                                          .:::.....'"':::::::'",...;::::;.
                                         ;:' '""'"";.,;:::::;.'""""""  ':;
                                        ::'         ;::;:::;::..         :;
                                       ::         ,;:::::::::::;:..       ::
                                       ;'     ,;;:;::::::::::::::;";..    ':.
                                      ::     ;:"  ::::::"""'::::::  ":     ::
                                       :.    ::   ::::::;  :::::::   :     ;
                                        ;    ::   :::::::  :::::::   :    ;
                                         '   ::   ::::::....:::::'  ,:   '
                                          '  ::    :::::::::::::"   ::
                                             ::     ':::::::::"'    ::
                                             ':       """""""'      ::
                                              ::                   ;:
                                              ':;                 ;:"


                                                     P_r_A_d_Y
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define endl "\\n"
#define mod 1000000007
#define inf 1e18
const int N = 300005;
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
void cs(int &x) { cout << "Case #" << x << ": "; }
bool cmp(int a, int b) { return a > b; }
int32_t main() {
  fastIO;
  // int t;cin>>t;
  // int x=1;
  // while(t--)
  // {
  int n;
  cin >> n;
  int c = 0;

  if (n % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }

  int num = 0;
  while (num < n) {
    num *= 10;
    num += 7;
    c++;
  }
  // cout<<c<<endl;
  while (1) {
    int d = num % n;
    if (d == 0)
      break;
    while (d < n) {
      d *= 10;
      d += 7;
      c++;
    }
    num = d;
    if (c > 1000000)
      break;
  }
  if (c > 1000000)
    cout << -1 << endl;
  else
    cout << c << endl;

  //}
  return 0;
}

Error: TLE

Fixed submission:

/*
                                             ::                     ::
                                             ':                     :
                                              :.                    :
                                           ;' ::                   ::  '
                                          .'  ';                   ;'  '.
                                         ::    :;                 ;:    ::
                                         ;      :;.             ,;:     ::
                                         :;      :;:           ,;"      ::
                                         ::.      ':;  ..,.;  ;:'     ,.;:
                                          "'"...   '::,::::: ;:   .;.;""'
                                              '"""....;:::::;,;.;"""
                                          .:::.....'"':::::::'",...;::::;.
                                         ;:' '""'"";.,;:::::;.'""""""  ':;
                                        ::'         ;::;:::;::..         :;
                                       ::         ,;:::::::::::;:..       ::
                                       ;'     ,;;:;::::::::::::::;";..    ':.
                                      ::     ;:"  ::::::"""'::::::  ":     ::
                                       :.    ::   ::::::;  :::::::   :     ;
                                        ;    ::   :::::::  :::::::   :    ;
                                         '   ::   ::::::....:::::'  ,:   '
                                          '  ::    :::::::::::::"   ::
                                             ::     ':::::::::"'    ::
                                             ':       """""""'      ::
                                              ::                   ;:
                                              ':;                 ;:"


                                                     P_r_A_d_Y
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define endl "\\n"
#define mod 1000000007
#define inf 1e18
const int N = 300005;
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
void cs(int &x) { cout << "Case #" << x << ": "; }
bool cmp(int a, int b) { return a > b; }
int32_t main() {
  fastIO;
  // int t;cin>>t;
  // int x=1;
  // while(t--)
  // {
  int n;
  cin >> n;
  int c = 0;

  if (n % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }

  int num = 0;
  while (num < n) {
    num *= 10;
    num += 7;
    c++;
  }
  // cout<<c<<endl;
  while (1) {
    int d = num % n;
    if (d == 0)
      break;
    while (d < n) {
      d *= 10;
      d += 7;
      c++;
    }
    num = d;
  }

  cout << c << endl;

  //}
  return 0;
}