#include <bits/stdc++.h>
using namespace std;
map<int, int> mii;
const int N = 1000;
int a;
int sum(int x) {
  int ret = 0;
  while (x) ret += x % 10, x /= 10;
  return ret;
}
int diff(int x1, int x2) {
  int ret = 0;
  while (x1 || x2) {
    if (x1 % 10 != x2 % 10) ++ret;
    x1 /= 10, x2 /= 10;
  }
  return ret;
}
int main() {
  scanf("%d", &a);
  int ans = 10;
  for (int(i) = (0); (i) <= (int)(N - 1); (i)++)
    for (int(j) = (0); (j) <= (int)(N - 1); (j)++) {
      int sumI = sum(i), sumJ = sum(j);
      if (sumI != sumJ) continue;
      ans = min(ans, diff(i * 1000 + j, a));
    }
  printf("%d\n", ans);
  return 0;
}
