Problem p03437 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define Abs(x) ((x) < 0 ? (x) * -1 : (x))
#define Max(x, y) (max(decltype(x)(0), max((x), (y))))
#define Min(x, y) (max(decltype(x)(0), min((x), (y))))
#define rep(x, y) for ((x) = 0; (x) < (y); (x)++)
#define repin(x, y) for ((x) = 0; (x) <= (y); (x)++)
#define nep(x, y) for ((x) = (y)-1; 0 <= (x); (x)--)
#define nepi(x, y, z) for ((x) = (y)-1; (z) <= (x); (x)--)
#define repi(x, y, z) for ((x) = (z); (x) < (y); (x)++)
#define repiin(x, y, z) for ((x) = (z); (x) <= (y); (x)++)
#define reps(x, y, z) for ((x) = 0; (x) < (y); (x) += (z))
#define repit(x, y) for ((x) = (y).begin(); (x) != (y).end(); (x)++)
#define all(x) (x).begin(), (x).end()
#define _end '\n'
#define Out(x) cout << (x) << _end
template <typename T> inline bool _max(T &x, const T &y) {
  return x < y ? x = y, 1 : 0;
}
template <typename T> inline bool _min(T &x, const T &y) {
  return x > y ? x = y, 1 : 0;
}
template <typename T> using vvec = vector<vector<T>>;

#define pb push_back
#define mp make_pair

static const ll MOD = 1e9 + 7;
static const double PI = 3.141592653589793;

#define LOCAL 0

int main() {
#if LOCAL
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  freopen("debug.txt", "w", stderr);
#endif

  cin.tie(0);
  ios::sync_with_stdio(false);

  ll x, y, len = 1e18;

  cin >> x >> y;

  if (x == y) {
    Out(-1);
  } else {
    if (x % y != 0) {
      Out(x);
    } else {
      for (ll i = y + x; i <= len; i += x) {
        if (i % y != 0) {
          Out(i);

          return 0;
        }
      }

      Out(-1);
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define Abs(x) ((x) < 0 ? (x) * -1 : (x))
#define Max(x, y) (max(decltype(x)(0), max((x), (y))))
#define Min(x, y) (max(decltype(x)(0), min((x), (y))))
#define rep(x, y) for ((x) = 0; (x) < (y); (x)++)
#define repin(x, y) for ((x) = 0; (x) <= (y); (x)++)
#define nep(x, y) for ((x) = (y)-1; 0 <= (x); (x)--)
#define nepi(x, y, z) for ((x) = (y)-1; (z) <= (x); (x)--)
#define repi(x, y, z) for ((x) = (z); (x) < (y); (x)++)
#define repiin(x, y, z) for ((x) = (z); (x) <= (y); (x)++)
#define reps(x, y, z) for ((x) = 0; (x) < (y); (x) += (z))
#define repit(x, y) for ((x) = (y).begin(); (x) != (y).end(); (x)++)
#define all(x) (x).begin(), (x).end()
#define _end '\n'
#define Out(x) cout << (x) << _end
template <typename T> inline bool _max(T &x, const T &y) {
  return x < y ? x = y, 1 : 0;
}
template <typename T> inline bool _min(T &x, const T &y) {
  return x > y ? x = y, 1 : 0;
}
template <typename T> using vvec = vector<vector<T>>;

#define pb push_back
#define mp make_pair

static const ll MOD = 1e9 + 7;
static const double PI = 3.141592653589793;

#define LOCAL 0

int main() {
#if LOCAL
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  freopen("debug.txt", "w", stderr);
#endif

  cin.tie(0);
  ios::sync_with_stdio(false);

  ll x, y, len = 1e18;

  cin >> x >> y;

  if (x == y) {
    Out(-1);
  } else {
    if (x % y != 0) {
      Out(x);
    } else {
      Out(-1);
    }
  }

  return 0;
}