#include <bits/stdc++.h>
using namespace std;
const long long int N = 1e5 + 5, MOD = 1000000007;
string str;
long long int ans1, ans2, suml, sumr, dp[7][30][30][30][30];
long long int calc(int i, int sl, int sr, int prel, int prer) {
  if (i == 6 && sl == sr) return 0;
  if (sl == sr) return 0;
  if (prer > prel && sl > sr) return 0;
  if (prer < prel && sl < sr) return 0;
  if (i == 6) return 1e18;
  if (dp[i][sl][sr][prer][prer]) return dp[i][sl][sr][prel][prer];
  if (abs(sl - sr) > 30) return 1e18;
  for (int j = 0; j < 6; j++) {
    if (i < 3)
      return dp[i][sl][sr][prel][prer] =
                 min(calc(i + 1, sl, sr, sl, sr),
                     min(1 + calc(i + 1, sl + (9 - (str[i] - '0')), sr, sl, sr),
                         1 + calc(i + 1, sl - (str[i] - '0'), sr, sl, sr)));
    else
      return dp[i][sl][sr][prel][prer] =
                 min(calc(i + 1, sl, sr, sl, sr),
                     min(1 + calc(i + 1, sl, sr + (9 - (str[i] - '0')), sl, sr),
                         1 + calc(i + 1, sl, sr - (str[i] - '0'), sl, sr)));
  }
}
int main() {
  cin >> str;
  sort(str.begin(), str.begin() + 3);
  sort(str.begin() + 3, str.end());
  for (int i = 0; i < 6; i++)
    if (i < 3)
      suml += (str[i] - '0');
    else
      sumr += (str[i] - '0');
  cout << calc(0, suml, sumr, suml, sumr);
  return 0;
}
