Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <bitset>
#include <chrono>
#include <climits>
#include <cmath>
#include <deque>
#include <iostream>
#include <locale>
#include <map>
#include <optional>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using i32 = int32_t;
using i64 = uint64_t;
using u32 = uint32_t;
using u64 = uint64_t;
template <typename T> T input_value() {
  T result;
  std::cin >> result;
  return result;
}

template <typename T1, typename T2>
void input_multival_to_array(const T1 &n, T2 &array) {
  for (T1 i = T1(0); i < n; ++i) {
    array.emplace_back(input_value<typename T2::value_type>());
  }
}

template <typename T> std::set<T> factorize(const T &n) {
  const T sqrtn = T(sqrt(double(n)));
  std::set<T> result = {T(1), T{n}};
  for (T i = T(2); i <= sqrtn; ++i) {
    if (n % i == T(0)) {
      result.insert(i);
      result.insert(n / i);
    }
  }
}

int main() {
  using namespace std;
#if _WIN32
  locale::global(locale(""));
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  const auto n_town = input_value<u32>();
  const auto n_teleports = input_value<u64>();
  vector<u32> dests;
  input_multival_to_array(n_town, dests);
  for_each(dests.begin(), dests.end(), [](u32 &d) { --d; });

  vector<u32> routes_from_1st;
  unordered_set<u32> visited;
  u64 loop_start_index = 0;
  u32 current_town = 0;
  for (; visited.find(current_town) == visited.end();
       current_town = dests[current_town]) {
    visited.insert(current_town);
    routes_from_1st.push_back(current_town);
  }
  loop_start_index = distance(
      routes_from_1st.begin(),
      find(routes_from_1st.begin(), routes_from_1st.end(), current_town));

  u64 loop_size = routes_from_1st.size() - loop_start_index;

  if (n_teleports < routes_from_1st.size()) {
    cout << (routes_from_1st[n_teleports] + 1) << '\\n';
  } else {
    cout << (routes_from_1st[(n_teleports - loop_start_index) % loop_size +
                             loop_start_index] +
             1)
         << '\\n';
  }
  return 0;
}


Error: -8

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <chrono>
#include <climits>
#include <cmath>
#include <deque>
#include <iostream>
#include <locale>
#include <map>
#include <optional>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using i32 = int32_t;
using i64 = uint64_t;
using u32 = uint32_t;
using u64 = uint64_t;
template <typename T> T input_value() {
  T result;
  std::cin >> result;
  return result;
}

template <typename T1, typename T2>
void input_multival_to_array(const T1 &n, T2 &array) {
  for (T1 i = T1(0); i < n; ++i) {
    array.emplace_back(input_value<typename T2::value_type>());
  }
}

template <typename T> std::set<T> factorize(const T &n) {
  const T sqrtn = T(sqrt(double(n)));
  std::set<T> result = {T(1), T{n}};
  for (T i = T(2); i <= sqrtn; ++i) {
    if (n % i == T(0)) {
      result.insert(i);
      result.insert(n / i);
    }
  }
}

int main() {
  using namespace std;
#if _WIN32
  locale::global(locale(""));
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  const auto n_town = input_value<u32>();
  const auto n_teleports = input_value<u64>();
  vector<u32> dests;
  input_multival_to_array(n_town, dests);
  for_each(dests.begin(), dests.end(), [](u32 &d) { --d; });

  vector<u32> routes_from_1st;
  unordered_set<u32> visited;
  u64 loop_start_index = 0;
  u32 current_town = 0;
  for (; visited.find(current_town) != visited.end();
       current_town = dests.at(current_town)) {
    visited.insert(current_town);
    routes_from_1st.push_back(current_town);
  }
  loop_start_index = distance(
      routes_from_1st.begin(),
      find(routes_from_1st.begin(), routes_from_1st.end(), current_town));

  u64 loop_size = routes_from_1st.size() - loop_start_index;

  if (n_teleports <= routes_from_1st.size()) {
    cout << (routes_from_1st[n_teleports - 1] + 1) << '\\n';
  } else {
    cout << (routes_from_1st[(n_teleports - loop_start_index) % loop_size +
                             loop_start_index - 1] +
             1)
         << '\\n';
  }
  return 0;
}