Language: C++

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <numeric>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
const double pi = acos(-1);

struct Edge {
    ll to, cost;
    Edge(ll to, ll cost): to(to), cost(cost) {}
};

struct Data {
    ll v, i, x;  // いまいる頂点v, 進んだ距離を3で割った余りi, 進んだ距離x
    Data(ll v, ll x): v(v), i(i), x(x) {}
    bool operator<(const Data& hoge) const {
        return x > hoge.x;
    }
};

int main() {
    ll n, m;
    cin >> n >> m;    
    
    vector<vector<Edge>> G(n);
    rep(i, m) {
        ll a, b;
        cin >> a >> b;
        a--;  b--;
        G[a].emplace_back(b, 1);
        G[b].emplace_back(a, 1);
    }

    ll sv, gv;
    cin >> sv >> gv;
    sv--;  gv--;

    vvl dp(n, vl(3, INF));  // dp[v][i] : 頂点vまでの3で割った余りがiとなる距離の最小値
    priority_queue<Data> Q;
    auto push = [&](ll v, ll i, ll x) {
        if(dp[v][i] <= x) {
            return;
        }
        dp[v][i] = x;
        Q.emplace(v, i, x);
    };
    
    // 拡張ダイクストラ法。計算量はO((V+E)*log(V))
    push(sv, 0, 0);
    while(!Q.empty()) {
        Data hoge = Q.top();  Q.pop();
        ll v = hoge.v, i = hoge.i, x = hoge.x;
        if(dp[v][i] != x) {
            continue;
        }
        for(Edge e : G[v]) {
            push(e.to, (x + e.cost)%3, x + e.cost);
        }
    }

    if(dp[gv][0] == INF) {
        out(-1);
    }
    else {
        out(dp[gv][0]/3);
    }
    
    re0;
}

Compiler output:
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/c++allocator.h:33,
                 from /usr/include/c++/14/bits/allocator.h:46,
                 from /usr/include/c++/14/string:43,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/new_allocator.h: In instantiation of 'void std::__new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = Data; _Args = {long long int&, long long int&, long long int&}; _Tp = Data]':
/usr/include/c++/14/bits/alloc_traits.h:575:17:   required from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = Data; _Args = {long long int&, long long int&, long long int&}; _Tp = Data; allocator_type = std::allocator<Data>]'
  575 |           __a.construct(__p, std::forward<_Args>(__args)...);
      |           ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/vector.tcc:117:30:   required from 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {long long int&, long long int&, long long int&}; _Tp = Data; _Alloc = std::allocator<Data>; reference = Data&]'
  117 |             _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
      |             ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  118 |                                      std::forward<_Args>(__args)...);
      |                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_queue.h:754:18:   required from 'void std::priority_queue<_Tp, _Sequence, _Compare>::emplace(_Args&& ...) [with _Args = {long long int&, long long int&, long long int&}; _Tp = Data; _Sequence = std::vector<Data, std::allocator<Data> >; _Compare = std::less<Data>]'
  754 |           c.emplace_back(std::forward<_Args>(__args)...);
      |           ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:72:18:   required from here
   72 |         Q.emplace(v, i, x);
      |         ~~~~~~~~~^~~~~~~~~
/usr/include/c++/14/bits/new_allocator.h:191:11: error: no matching function for call to 'Data::Data(long long int&, long long int&, long long int&)'
  191 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:42:5: note: candidate: 'Data::Data(ll, ll)'
   42 |     Data(ll v, ll x): v(v), i(i), x(x) {}
      |     ^~~~
a.cc:42:5: note:   candidate expects 2 arguments, 3 provided
a.cc:40:8: note: candidate: 'constexpr Data::Data(const Data&)'
   40 | struct Data {
      |        ^~~~
a.cc:40:8: note:   candidate expects 1 argument, 3 provided
a.cc:40:8: note: candidate: 'constexpr Data::Data(Data&&)'
a.cc:40:8: note:   candidate expects 1 argument, 3 provided