#include "bits/stdc++.h"
#define LF '\n'
#define ALL(x) x.begin(), x.end()
#define iostreamBooster() { cin.tie(nullptr); ios_base::sync_with_stdio(false); }
using namespace std;
typedef int64_t i64;
typedef pair<int,int> pii;
template<class A, class B>inline bool chmax(A &a, const B &b){return b>a ? a=b,1 : 0;}
template<class A, class B>inline bool chmin(A &a, const B &b){return b<a ? a=b,1 : 0;}
constexpr int INF = 0x3f3f3f3f;
template<class Itr> void dump(Itr begin, Itr end) { for (;begin != end; ++begin) clog << *begin << LF; }

signed main()
{
  priority_queue< int , vector<int>, greater<int> > pq;
  int cnt[128] {};
  string s;

  cin >> s;

  for (char c : s) {
    ++cnt[c];
  }

  for (char i = 'a'; i <= 'z'; ++i) {
    if (cnt[i] > 0) pq.push(cnt[i]);
  }

  int ans = 0;
  if (pq.size() <= 1) pq.push(0);

  while(pq.size() > 1) {
    int l = pq.top(); pq.pop();
    int r = pq.top(); pq.pop();

    ans += l+r;
    pq.push(l+r);
  }

  cout << ans << LF;

  return 0;
}


