#include using namespace std; typedef long long ll; int main() { cin.tie(0); ios::sync_with_stdio(false); string s, goal; cin >> s; goal = s; sort(goal.begin(), goal.end()); map m; m[s] = 0; queue q; q.push(s); while(q.size()) { s = q.front(); q.pop(); int d = m[s]; for(int i = 0; i < s.size() - 1; i++) { string ns = s; swap(ns[i], ns[i + 1]); if(m.count(ns)) continue; m[ns] = d + 1; q.push(ns); } } cout << m[goal] << endl; }