Language: C++

#include <set>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int N, T;
    
    cin >> N;
    
    vector<char> A(N);
    
    for(int i = 0; i < N; i++) { cin >> T; A[i] = T; }
    
    vector<char> B = A; sort(B.begin(), B.end());
    
    set<vector<char>, bool> dat; dat.insert(A);
    
    queue<vector<char> > que; que.push(A);
    
    queue<char> que2; que2.push(0);
    
    if(A == B) { cout << 0 << endl; goto Exit; }
    
    while(que.size())
    {
        vector<char> v1 = que.front(); que.pop();
        
        int dist = que2.front(); que2.pop();
        
        for(int i = 0; i < N; i++)
        {
            for(int j = 0; j < N; j++)
            {
                vector<char> v2 = v1;
                
                swap(v2[i], v2[j]);
                
                if(v2 == B) { cout <<  dist + 1 << endl; goto Exit; }
                
                if(dat.find(v2) == dat.end())
                {
                    dat.insert(v2);
                    
                    que.push(v2);
                    
                    que2.push(dist + 1);
                }
            }
        }
    }
    
    Exit:;
    
    return 0;
}

Compiler output:
In file included from /usr/include/c++/14/set:62,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::find(const _Key&) [with _Key = std::vector<char>; _Val = std::vector<char>; _KeyOfValue = std::_Identity<std::vector<char> >; _Compare = bool; _Alloc = std::allocator<std::vector<char> >; iterator = std::_Rb_tree<std::vector<char>, std::vector<char>, std::_Identity<std::vector<char> >, bool, std::allocator<std::vector<char> > >::iterator]':
/usr/include/c++/14/bits/stl_set.h:797:25:   required from 'std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::find(const key_type&) [with _Key = std::vector<char>; _Compare = bool; _Alloc = std::allocator<std::vector<char> >; iterator = std::_Rb_tree<std::vector<char>, std::vector<char>, std::_Identity<std::vector<char> >, bool, std::allocator<std::vector<char> > >::const_iterator; key_type = std::vector<char>]'
  797 |       { return _M_t.find(__x); }
      |                ~~~~~~~~~^~~~~
a.cc:45:28:   required from here
   45 |                 if(dat.find(v2) == dat.end())
      |                    ~~~~~~~~^~~~
/usr/include/c++/14/bits/stl_tree.h:2532:40: error: expression cannot be used as a function
 2532 |               || _M_impl._M_key_compare(__k,
      |                  ~~~~~~~~~~~~~~~~~~~~~~^~~~~
 2533 |                                         _S_key(__j._M_node))) ? end() : __j;
      |                                         ~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_unique_pos(const key_type&) [with _Key = std::vector<char>; _Val = std::vector<char>; _KeyOfValue = std::_Identity<std::vector<char> >; _Compare = bool; _Alloc = std::allocator<std::vector<char> >; key_type = std::vector<char>]':
/usr/include/c++/14/bits/stl_tree.h:2169:4:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = const std::vector<char>&; _Key = std::vector<char>; _Val = std::vector<char>; _KeyOfValue = std::_Identity<std::vector<char> >; _Compare = bool; _Alloc = std::allocator<std::vector<char> >]'
 2169 |         = _M_get_insert_unique_pos(_KeyOfValue()(__v));
      |           ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:514:25:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = std::vector<char>; _Compare = bool; _Alloc = std::allocator<std::vector<char> >; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree<std::vector<char>, std::vector<char>, std::_Identity<std::vector<char> >, bool, std::allocator<std::vector<char> > >::const_iterator; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other = std::allocator<std::vector<char> >; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key> = __gnu_cxx::__alloc_traits<std::allocator<std::vector<char> >, std::vector<char> >::rebind<std::vector<char> >; typename _Alloc::value_type = std::vector<char>; value_type = std::vector<char>]'
  514 |           _M_t._M_insert_unique(__x);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~
a.cc:21:44:   required from here
   21 |     set<vector<char>, bool> dat; dat.insert(A);
      |                                  ~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_tree.h:2116:42: error: expression cannot be used as a function
 2116 |           __comp = _M_impl._M_key_compare(__k, _S_key(__x));
      |                    ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:2127:33: error: expression cannot be used as a function
 2127 |       if (_M_impl._M_key_compare(_S_key(__j._M_node), __k))
      |           ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_(_Base_ptr, _Base_ptr, _Arg&&, _NodeGen&) [with _Arg = const std::vector<char>&; _NodeGen = std::_Rb_tree<std::vector<char>, std::vector<char>, std::_Identity<std::vector<char> >, bool, std::allocator<std::vector<char> > >::_Alloc_node; _Key = std::vector<char>; _Val = std::vector<char>; _KeyOfValue = std::_Identity<std::vector<char> >; _Compare = bool; _Alloc = std::allocator<std::vector<char> >; iterator = std::_Rb_tree<std::vector<char>, std::vector<char>, std::_Identity<std::vector<char> >, bool, std::allocator<std::vector<char> > >::iterator; _Base_ptr = std::_Rb_tree_node_base*]':
/usr/include/c++/14/bits/stl_tree.h:2174:26:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = const std::vector<char>&; _Key = std::vector<char>; _Val = std::vector<char>; _KeyOfValue = std::_Identity<std::vector<char> >; _Compare = bool; _Alloc = std::allocator<std::vector<char> >]'
 2174 |           return _Res(_M_insert_(__res.first, __res.second,
      |                       ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
 2175 |                                  _GLIBCXX_FORWARD(_Arg, __v), __an),
      |                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:514:25:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = std::vector<char>; _Compare = bool; _Alloc = std::allocator<std::vector<char> >; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree<std::vector<char>, std::vector<char>, std::_Identity<std::vector<char> >, bool, std::allocator<std::vector<char> > >::const_iterator; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other = std::allocator<std::vector<char> >; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key> = __gnu_cxx::__alloc_traits<std::allocator<std::vector<char> >, std::vector<char> >::rebind<std::vector<char> >; typename _Alloc::value_type = std::vector<char>; value_type = std::vector<char>]'
  514 |           _M_t._M_insert_unique(__x);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~
a.cc:21:44:   required from here
   21 |     set<vector<char>, bool> dat; dat.insert(A);
      |                                  ~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_tree.h:1824:56: error: expression cannot be used as a function
 1824 |                               || _M_impl._M_key_compare(_KeyOfValue()(__v),
      |                                  ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
 1825 |                                                         _S_key(__p)));
      |                                                         ~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_lower_bound(_Link_type, _Base_ptr, const _Key&) [with _Key = std::vector<char>; _Val = std::vector<char>; _KeyOfValue = std::_Identity<std::vector<char> >; _Compare = bool; _Alloc = std::allocator<std::vector<char> >; iterator = std::_Rb_tree<std::vector<char>, std::vector<char>, std::_Identity<std::vector<char> >, bool, std::allocator<std::vector<char> > >::iterator; _Link_type = std::_Rb_tree_node<std::vector<char> >*; _Base_ptr = std::_Rb_tree_node_base*]':
/usr/include/c++/14/bits/stl_tree.h:2530:36:   required from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::find(const _Key&) [with _Key = std::vector<char>; _Val = std::vector<char>; _KeyOfValue = std::_Identity<std::vector<char> >; _Compare = bool; _Alloc = std::allocator<std::vector<char> >; iterator = std::_Rb_tree<std::vector<char>, std::vector<char>, std::_Identity<std::vector<char> >, bool, std::allocator<std::vector<char> > >::iterator]'
 2530 |       iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
      |                      ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:797:25:   required from 'std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::find(const key_type&) [with _Key = std::vector<char>; _Compare = bool; _Alloc = std::allocator<std::vector<char> >; iterator = std::_Rb_tree<std::vector<char>, std::vector<char>, std::_Identity<std::vector<char> >, bool, std::allocator<std::vector<char> > >::const_iterator; key_type = std::vector<char>]'
  797 |       { return _M_t.find(__x); }
      |                ~~~~~~~~~^~~~~
a.cc:45:28:   required from here
   45 |                 if(dat.find(v2) == dat.end())
      |                    ~~~~~~~~^~~~
/usr/include/c++/14/bits/stl_tree.h:1948:36: error: expression cannot be used as a function
 1948 |         if (!_M_impl._M_key_compare(_S_key(__x), __k))
      |              ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'static const _Key& std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_S_key(_Const_Link_type) [with _Key = std::vector<char>; _Val = std::vector<char>; _KeyOfValue = std::_Identity<std::vector<char> >; _Compare = bool; _Alloc = std::allocator<std::vector<char> >; _Const_Link_type = const std::_Rb_tree_node<std::vector<char> >*]':
/usr/include/c++/14/bits/stl_tree.h:2116:47:   required from 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_unique_pos(const key_type&) [with _Key = std::vector<char