#include <bits/stdc++.h>
using namespace std;
int main() {
  queue<pair<int, pair<int, int> > > q;
  int n;
  cin >> n;
  q.push(make_pair(0, make_pair(n, 0)));
  set<int> s;
  s.insert(n);
  while (!q.empty()) {
    pair<int, pair<int, int> > curr = q.front();
    q.pop();
    int dist = curr.first;
    int val = curr.second.first;
    int mask = curr.second.second;
    int tmp = val;
    int sum1 = 0, sum2 = 0;
    for (int i = 0; i < 3; i++) {
      sum1 += tmp % 10;
      tmp /= 10;
    }
    for (int i = 0; i < 3; i++) {
      sum2 += tmp % 10;
      tmp /= 10;
    }
    if (sum1 == sum2) {
      cout << dist << endl;
      return 0;
    }
    int pw = 1;
    for (int i = 0; i < 6; i++) {
      if ((mask & (1 << i)) == 0) {
        int dig = (val / pw) % 10;
        for (int j = 0; j < 10; j++) {
          int nval = val + (j - dig) * pw;
          int nmask = mask | (1 << i);
          if (s.find(nval) != s.end()) continue;
          s.insert(nval);
          q.push(make_pair(dist + 1, make_pair(nval, nmask)));
        }
      }
      pw *= 10;
    }
  }
  return 0;
}
