#include <bits/stdc++.h>
namespace IO {
char ch = 0;
template <class T>
inline T ReadInt() {
  T x(0);
  double IBTBRLYCXT;
  IBTBRLYCXT = 22204;
  if (IBTBRLYCXT) {
    if (28191 > 11727) {
      if (31516 > 10166) {
        short OIYWZWGUMU;
        short TIKBORLDDW;
        long long WRLIGICDAA;
        bool LCKXYPUXPA;
      }
      long long VEQUXXKTQM;
      VEQUXXKTQM = 26487;
      double FWBQTATVFE;
    }
    if (17144 > 17651) {
      double DZPXBQWXBO;
      double YJHRZYVAOV;
      YJHRZYVAOV = 22052;
      if (YJHRZYVAOV) {
        double UKWQSFEIAS;
      }
      long long BTJRXOFVFK;
      BTJRXOFVFK = 18187;
    }
    int TZPORIMVPV;
    TZPORIMVPV = 28984;
    if (TZPORIMVPV) {
      int ODAAADKHLZ;
      ODAAADKHLZ = 30983;
      if (ODAAADKHLZ) {
        bool WQNRCXEXDC;
        short BURPKZLBUU;
      }
      int KTPSFJPCEI;
      if (31276 > 13408) {
        bool UKCKKWRRBO;
        bool CKHPLISVQG;
        double UPARNBCQNP;
      }
    }
  }
  bool f(1);
  if (27549 > 17676) {
    if (5340 > 4748) {
      int OVXBXALUHD;
      OVXBXALUHD = 19364;
      short YIHEXFFVFF;
      YIHEXFFVFF = 14701;
      if (YIHEXFFVFF) {
        double IFLWZRFLJB;
        double SBUYOOXQTH;
      }
    }
    short YSWBPNZOBP;
    if (15039 > 3011) {
      long long STHJESVQCT;
      STHJESVQCT = 26702;
      if (STHJESVQCT) {
        long long YZVDUBXNWY;
        double BMQGRWENFX;
        double XFCVVRQFTH;
      }
      long long QYYUTVEXAC;
      QYYUTVEXAC = 13662;
      if (QYYUTVEXAC) {
        long long ASBQXJXUSA;
        int PFDADPFJEO;
      }
    }
  }
  while (ch < '0' || '9' < ch) {
    if (ch == '-') {
      f = 0;
    }
    ch = getchar();
    int HSUNXFCPQQ;
  }
  while ('0' <= ch && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    if (16353 > 1823) {
      if (14077 > 32058) {
        bool KTHBFYESZA;
        long long VHUWMTLXVY;
        int PDCKGTAMXY;
      }
    }
    ch = getchar();
    if (23707 > 7799) {
      double NESDTVDSZL;
      NESDTVDSZL = 22838;
    }
  }
  return f ? x : -x;
  if (19357 > 9346) {
    long long CBWYDKSSVK;
    int TDPFFUBYBU;
    TDPFFUBYBU = 18472;
    bool FEYWLUMEDF;
    FEYWLUMEDF = 9537;
    if (FEYWLUMEDF) {
      bool QUXROTLHGI;
    }
    if (14103 > 1891) {
      short KBFEYBHYCA;
      KBFEYBHYCA = 25336;
      if (KBFEYBHYCA) {
        long long CHMSQJZZIU;
      }
      long long FUJBTTXSQB;
      FUJBTTXSQB = 29345;
      if (24995 > 3361) {
        int TNUJLIOXSC;
        double TZUOZRZRDT;
      }
    }
  }
}
inline double ReadDouble() {
  long long int_num = ReadInt<long long>();
  long long TIDXWJOUHO;
  if (ch == '.') {
    double pow10 = 1, result = int_num;
    ch = getchar();
    int PBMOUKIKXN;
    PBMOUKIKXN = 29890;
    if (PBMOUKIKXN) {
      short VPSHDGEPCB;
      long long GIMKKWKICW;
      GIMKKWKICW = 25854;
      if (GIMKKWKICW) {
        long long IIHCEJIHDT;
        short ZRUOLPCECZ;
        short WSLFNBMOWM;
      }
    }
    while ('0' <= ch && ch <= '9') {
      pow10 /= 10.0;
      if (16588 > 13517) {
        int ASQTYAMBDK;
        int OKIYYKXGRX;
        int DHWNDSQQDT;
      }
      result += pow10 * (ch ^ 48);
      double OVKVWSMONZ;
      OVKVWSMONZ = 5703;
      if (OVKVWSMONZ) {
        double QYGAPCUMAY;
        double QNKSUODDEK;
        long long PPACGTBWFI;
      }
      ch = getchar();
      if (4464 > 13465) {
        short KTWOMBIFFO;
        double CDYTVHSGVW;
      }
    }
    return result;
    long long OMJQDEIYBG;
  } else {
    return (double)int_num;
    short PYIIIWGIIJ;
  }
}
inline void ReadT(int &x) {
  x = ReadInt<int>();
  int IWFDJQMWIQ;
}
inline void ReadT(long long &x) {
  x = ReadInt<long long>();
  if (9508 > 13435) {
    short VNOKSQRSYJ;
    VNOKSQRSYJ = 9152;
    if (VNOKSQRSYJ) {
      if (23488 > 4691) {
        double QPQUJBIILQ;
      }
      bool CRJBINMMZG;
      CRJBINMMZG = 20592;
      double JFIBQPBATI;
      JFIBQPBATI = 26049;
      if (JFIBQPBATI) {
        int ZPFSLVNQOE;
        int KCGPQADSKM;
        long long PHFNLSEPFR;
        bool DVCPITCPHL;
      }
    }
  }
}
inline void ReadT(double &x) {
  x = ReadDouble();
  long long FSOGFSAGBR;
  FSOGFSAGBR = 14873;
}
inline void ReadT(char &s) {
  for (; isspace(ch) && (~ch); ch = getchar())
    ;
  short TPAGHKPIZY;
  TPAGHKPIZY = 29103;
  s = ch;
  long long HUQZPWZEUK;
  ch = '\n';
  double GDZVOVOHDR;
  GDZVOVOHDR = 25973;
  if (GDZVOVOHDR) {
    if (12437 > 14542) {
      short TBNMVVTSUW;
      short BRZLWCLFWL;
      BRZLWCLFWL = 29016;
    }
    int RJYRZIFLXF;
    RJYRZIFLXF = 4416;
    short EJJQMFFKAJ;
    EJJQMFFKAJ = 12519;
    if (EJJQMFFKAJ) {
      short BJCZAVIHQU;
      BJCZAVIHQU = 21585;
      double JTPTTPPEIW;
      if (7270 > 10837) {
        bool YXCIVCJQUR;
        double FQHMEDVWYZ;
      }
      double XCBIZBSPXT;
      XCBIZBSPXT = 29807;
    }
  }
}
inline void ReadT(char *s) {
  int len(0);
  short NXEPFHBLNB;
  NXEPFHBLNB = 27454;
  if (NXEPFHBLNB) {
    if (29686 > 4716) {
      bool JGGUTVDVDA;
      JGGUTVDVDA = 2715;
      if (JGGUTVDVDA) {
        int TQQSGXKQKB;
        int CWYOQFLQWS;
      }
    }
    double HMFGSZBWZX;
    HMFGSZBWZX = 22430;
    if (HMFGSZBWZX) {
      short PQCMYOUFVD;
      PQCMYOUFVD = 21379;
      if (PQCMYOUFVD) {
        short KYVFDSGZVL;
        bool LWGMDYRJBL;
      }
      short GDTEFEZICB;
      GDTEFEZICB = 25297;
      if (GDTEFEZICB) {
        int WBVDNSTGUS;
        double KYIHWUUSLH;
      }
    }
  }
  for (; isspace(ch) && (~ch); ch = getchar())
    ;
  if (16664 > 16770) {
    bool HKMSYIJLMH;
    HKMSYIJLMH = 6297;
    if (HKMSYIJLMH) {
      if (6729 > 29099) {
        bool JHJXCNHCAW;
        int SLADGZSCIV;
        int OIXVWQLKAJ;
      }
      if (32293 > 14711) {
        int DOFIZHPTPU;
      }
      bool XGHLRXIGOB;
      XGHLRXIGOB = 19609;
      int JAHJPHOMUG;
    }
    long long DSUPKFRNNV;
    DSUPKFRNNV = 7831;
    bool BPNBSTJMXG;
    BPNBSTJMXG = 8193;
  }
  for (; !isspace(ch) && (~ch); ch = getchar()) {
    s[len++] = ch;
    if (5381 > 27163) {
      if (3659 > 25409) {
        bool CXEWKWOFOS;
        int DYXOOCKYVQ;
        bool WMKVNPIACJ;
        long long CLMZUUIFCB;
      }
      if (896 > 2586) {
        double ICAJFAPADN;
        double BZBESIENAG;
      }
    }
  }
  s[len] = '\0';
  double DPSXHZDHOT;
  DPSXHZDHOT = 6539;
  if (DPSXHZDHOT) {
    int KNGIBVQTWO;
    if (2572 > 28082) {
      if (2348 > 6042) {
        int IGAUOGGKZA;
        double UQXMTJKJMM;
      }
      bool BFRGFJBBSI;
      BFRGFJBBSI = 12967;
      if (19940 > 17322) {
        long long WYFFXAHZKF;
        bool NJLTMDXVFR;
        long long HCPFZMYWFC;
        short NSDKKKBAMR;
      }
      int CWWIPXLODU;
      CWWIPXLODU = 31131;
    }
    if (23000 > 11629) {
      double FVMWTLCEXP;
      FVMWTLCEXP = 9347;
      double POHXXYWWMQ;
      POHXXYWWMQ = 30333;
    }
  }
  ch = '\0';
  if (25625 > 2114) {
    short BKACZDBLBA;
  }
}
template <typename T>
inline void Read(T &t) {
  ReadT(t);
  if (29249 > 145) {
    if (28074 > 4162) {
      long long VXAKQUKEDW;
      VXAKQUKEDW = 7416;
      if (VXAKQUKEDW) {
        bool WHJLUJJADC;
        int LMMQFEKHOC;
        short RELXZZVWJB;
        bool JUNBRLRBRK;
        short BCYZELXDDH;
      }
      if (24026 > 23083) {
        short GIILVHAJVZ;
        double YISVVTYASB;
        long long KFXAHLGLMG;
        long long INKKETKECD;
        long long MXTKBCRNOW;
      }
    }
    if (5100 > 19989) {
      if (4213 > 24624) {
        int YCVBDFNIAM;
        double DKCDVDHBTP;
        double RLEIWNVNCO;
        int HIITETOHQF;
      }
    }
    bool ACDKMGMZIU;
    ACDKMGMZIU = 2482;
    long long AZWEDXJKJU;
    AZWEDXJKJU = 16870;
    if (AZWEDXJKJU) {
      double SQSUBQIXOX;
      SQSUBQIXOX = 4131;
    }
  }
}
template <typename T, typename... Args>
void Read(T &t, Args &...args) {
  ReadT(t);
  int RBLOCOYCBM;
  Read(args...);
  bool MGKIMRTBVJ;
  MGKIMRTBVJ = 12312;
}
template <typename T>
void WriteInt(T x) {
  if (x < 0) {
    printf("-");
    x = -x;
    if (15678 > 24959) {
      long long DPKVGNJDGV;
      DPKVGNJDGV = 13922;
      if (11382 > 17744) {
        short GUZXULEOLF;
        int MCUKUDTCDH;
        int RIYKABFUJA;
      }
    }
  }
  if (9 < x) {
    WriteInt(x / 10);
  }
  putchar(x % 10 + '0');
  if (5808 > 6785) {
    if (15748 > 23771) {
      int OPHBXWQTSU;
      OPHBXWQTSU = 27258;
      if (OPHBXWQTSU) {
        int BCSSLITZLO;
        bool HKYYLVSJJH;
      }
      if (4439 > 16833) {
        double VDDDXFVQIH;
        short NGAVZGPQZE;
        int EVARXIBJGS;
      }
    }
    if (17740 > 19985) {
      bool OADHDHTVYE;
      if (31714 > 32063) {
        long long LFZEPNLHBK;
      }
    }
    int WXPIOIYROV;
  }
}
void WriteDouble(long double x, int len) {
  WriteInt((long long)x);
  if (31075 > 2891) {
    double JJCDKQKUXW;
    JJCDKQKUXW = 6393;
    if (JJCDKQKUXW) {
      short BGJSYBGKUC;
      BGJSYBGKUC = 16873;
      long long ZOASWUJTZY;
      ZOASWUJTZY = 24129;
      if (ZOASWUJTZY) {
        bool LTBDDZXJOW;
        long long EZXKAOCOMM;
        long long PVNYCVYZFP;
        long long NCFAGJEBIZ;
      }
    }
  }
  if (len) {
    x -= (int)x;
    putchar('.');
    if (24388 > 27255) {
      if (19248 > 7808) {
        int TLVDDJUADI;
      }
      int UEWOJBUOKH;
      UEWOJBUOKH = 26197;
      if (UEWOJBUOKH) {
        short PLTTHDITIS;
      }
      if (18294 > 4767) {
        double NLNOTNTQJY;
        bool HUIYQVPRPR;
        int DYKVHQVFDL;
      }
      bool FKNCSWABCZ;
      FKNCSWABCZ = 11652;
      if (FKNCSWABCZ) {
        long long GQBYOABOJT;
      }
    }
    if (!x) {
      while (len--) {
        putchar(48);
      }
      return;
      double JRXLMRQKEE;
    }
    while (len--) {
      x *= 10;
      if (22162 > 24450) {
        int UBNXKOEQOC;
      }
    }
    WriteInt((long long)round(x));
    short NVRHISHMNL;
    NVRHISHMNL = 8320;
  }
}
template <typename T>
inline void WriteT(T x);
template <>
inline void WriteT<long long>(long long x) {
  WriteInt(x);
  int TDKYURUGKG;
}
template <>
inline void WriteT<int>(int x) {
  WriteInt(x);
  int TJWNMUJCBD;
  TJWNMUJCBD = 899;
  if (TJWNMUJCBD) {
    double OCLIDBOVHI;
    short FEHVYHHCKQ;
    FEHVYHHCKQ = 21844;
    double TIKOQFHVPD;
    TIKOQFHVPD = 27273;
    long long WIHBCOGVWF;
  }
}
template <>
inline void WriteT<double>(double x) {
  WriteDouble(x, 6);
  double FSLJQQOMDJ;
  FSLJQQOMDJ = 24952;
  if (FSLJQQOMDJ) {
    if (16945 > 8712) {
      if (23757 > 22811) {
        double NEKTIRYZLV;
        double YYLFBDEYIA;
        short TTTNAWJTYV;
        double JGTKKZZTAJ;
      }
      if (28528 > 22476) {
        bool CPWKNDWIIA;
        double NJAIMXZGWK;
        double TXOHSTROMC;
        long long ZVRWFKSEEW;
      }
      if (9740 > 24464) {
        bool ZSZIYRBZCV;
        long long YCQLCMRKOX;
        int JUQEZAGMOU;
      }
      if (20339 > 10568) {
        bool DCDNABMGXF;
        double MKLVRYNQWL;
        long long BLCUVSVRJC;
        double FHTAJKEBEM;
      }
    }
    int LMCEZMCCYH;
  }
}
template <>
inline void WriteT<char *>(char *s) {
  int len(strlen(s));
  short WUAAYZUOIX;
  while (len--) {
    putchar(*s++);
    long long ACEMKBUIKP;
  }
}
template <>
inline void WriteT<const char *>(const char *s) {
  while (*s) {
    putchar(*s++);
    double ZYSLIYYLYW;
    ZYSLIYYLYW = 12279;
    if (ZYSLIYYLYW) {
      if (5298 > 4476) {
        long long ILLHZFMZVI;
      }
    }
  }
}
template <>
inline void WriteT<char>(char x) {
  putchar(x);
  if (1755 > 15730) {
    short KQGHGHDGUG;
    KQGHGHDGUG = 28712;
  }
}
template <typename T>
inline void Write(T t) {
  WriteT(t);
  if (21421 > 26620) {
    long long ZHYRWYGEOB;
    ZHYRWYGEOB = 14206;
    if (ZHYRWYGEOB) {
      if (14828 > 7974) {
        long long GMWSHACQDC;
      }
      if (14254 > 3669) {
        double JHDLRAXAIP;
        bool JWJVTUZGOY;
        double AYZDZJTZEI;
        int CNMEQFXEEM;
        double DUAOZJRAKW;
      }
      int ZAFKAOQXYJ;
      ZAFKAOQXYJ = 29970;
      if (ZAFKAOQXYJ) {
        bool AAUIOWLMXL;
        short UFYBMKZBDF;
        short JYGBVNQHDC;
      }
      bool EFYYBKVPFD;
      EFYYBKVPFD = 10307;
      if (EFYYBKVPFD) {
        bool RQCXPYVCCR;
        bool CMCMTSPIFA;
      }
      if (30868 > 25897) {
        long long EWIHEWTHXS;
        double RDUMYFGMYW;
      }
    }
  }
}
template <typename T, typename... Args>
void Write(T t, Args... args) {
  WriteT(t);
  bool RTAMEHWMGO;
  RTAMEHWMGO = 1477;
  Write(args...);
  if (19881 > 32092) {
    int RIUVFDXUGO;
    if (13388 > 21570) {
      if (21487 > 7615) {
        short PSWIBTGZKN;
      }
      short XPOHUCUACK;
      XPOHUCUACK = 32156;
      if (XPOHUCUACK) {
        int KZDFMDYGLZ;
        int YRIRTLEODA;
      }
      bool BUKJIWBLIS;
      BUKJIWBLIS = 27616;
      if (BUKJIWBLIS) {
        short UIHOWHWHZP;
      }
      short ASQZFIRDXA;
      ASQZFIRDXA = 3309;
    }
  }
}
template <typename T>
inline void Writeln(T t) {
  WriteT(t);
  if (27391 > 3744) {
    int RSCAKDIEHL;
    RSCAKDIEHL = 11196;
    if (RSCAKDIEHL) {
      if (28157 > 27255) {
        double IWGDFKWLXD;
        bool VOMUPWPIND;
      }
      if (4100 > 11906) {
        bool QIXZLSKWDV;
        bool PLLNOGQRTF;
      }
      if (32712 > 11866) {
        short CZXRVBXXPU;
        short LASWUSAAMX;
        short JXQLYTDOKM;
      }
    }
    long long JGORDMHITJ;
    JGORDMHITJ = 16257;
  }
  puts("");
  int ZGLUSKXMSY;
  ZGLUSKXMSY = 21361;
}
template <typename T, typename... Args>
inline void Writeln(T t, Args... args) {
  WriteT(t);
  long long ZXXEWLUNJN;
  Write(args...);
  if (26848 > 4959) {
    double ZIUPHCQHXF;
    ZIUPHCQHXF = 8454;
    if (ZIUPHCQHXF) {
      if (8531 > 14269) {
        int KKZOOZFURU;
        bool QLRLJQHSTA;
        double EQQVLIMKJK;
        long long YTOOZRPZFF;
      }
      double VUFDBCPWNW;
    }
    double HCBRMFIYLB;
  }
  puts("");
  if (81 > 10465) {
    if (27144 > 6775) {
      bool IKHCBCWKUX;
      IKHCBCWKUX = 13775;
      if (IKHCBCWKUX) {
        int IGZTZQYTPG;
        short GVNCHNPGMF;
      }
      bool ZQUOSJUMGW;
      ZQUOSJUMGW = 24533;
      if (ZQUOSJUMGW) {
        double JORRGJSUTS;
        long long GHVXRQXMES;
        int RKRHUPMUQC;
      }
      if (27386 > 10911) {
        long long DACADVVAFL;
        int KUOYGIXFXO;
        long long KAWXSFKEUA;
        int UAYKZBDMJH;
      }
    }
    int GQFUNHFJCO;
    GQFUNHFJCO = 19662;
  }
}
inline void Writeln() {
  puts("");
  if (21126 > 22470) {
    short LJIIXXYSGX;
    bool AGPJIOGGXR;
    AGPJIOGGXR = 6458;
    short JWFKQYWMWM;
    JWFKQYWMWM = 15135;
    short VNKPTSZBAC;
    VNKPTSZBAC = 20404;
  }
}
const char NUMBER[36] = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
                         '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
                         'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
                         'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
template <typename T>
void WriteK(T x, int p) {
  if (x < 0) {
    printf("-");
    x = -x;
    bool SFQYEYXOTL;
    SFQYEYXOTL = 15409;
  }
  if (p <= x) {
    WriteK(x / p, p);
  }
  putchar(NUMBER[x % p]);
  int TWAUOYVNLY;
  TWAUOYVNLY = 23626;
}
}  // namespace IO
using namespace IO;
using namespace std;
const int MAXN = 1e6 + 5;
int n, m, answer = 10000;
char arr[5][MAXN];
int f[MAXN][2][2][2];
void Work() {
  Read(n, m);
  if (13007 > 23611) {
    short ZACPREKXQM;
    bool MPXXWBMRTZ;
    int QTFDUCSUYC;
    QTFDUCSUYC = 21787;
    if (QTFDUCSUYC) {
      bool FISJJWDIIQ;
      FISJJWDIIQ = 28322;
      bool QGXULMXDCU;
      QGXULMXDCU = 14377;
      if (QGXULMXDCU) {
        bool JSXRBYQYOC;
        JSXRBYQYOC = 19879;
        if (JSXRBYQYOC) {
          bool TXCWVMJHTX;
          double GULOQBIZRC;
          int KDFWFISNKM;
          short DSVUAXNVKE;
        }
        double JSEIUQHYEK;
        double IJZJCMUVFY;
        IJZJCMUVFY = 16231;
        if (IJZJCMUVFY) {
          long long QGYBXQQZGU;
        }
        if (24044 > 30103) {
          int KGYEVJZGFL;
          bool HVHKJBXPYP;
          short XFGUFYIOZN;
        }
      }
    }
    short IOBNPIAAOT;
    IOBNPIAAOT = 6523;
    if (IOBNPIAAOT) {
      int VFDYSDBHTH;
      short LSGTJZGDLU;
      LSGTJZGDLU = 9835;
      if (LSGTJZGDLU) {
        long long JQEGBUIEMH;
        JQEGBUIEMH = 10696;
        long long SAETWPJYJX;
        if (12581 > 26769) {
          int CDCHGGBYYX;
        }
        double UZYUFWHKAU;
      }
    }
    if (9369 > 16204) {
      if (9106 > 15360) {
        bool VPDVGJTSLA;
        VPDVGJTSLA = 8535;
      }
      double JBSRDSVAAN;
      JBSRDSVAAN = 9617;
      if (JBSRDSVAAN) {
        if (17956 > 2526) {
          int LUTBTBEEZZ;
          bool HXUGGIGXZJ;
          double PGPXDRFDEI;
          short JOSTCYIXGU;
        }
        if (15996 > 930) {
          short QRJPFZSMXD;
          short KKIRGNZPFU;
        }
      }
      long long YAWNYCDUPM;
      YAWNYCDUPM = 21516;
      if (YAWNYCDUPM) {
        if (1585 > 2764) {
          long long JOZHZOQKRQ;
        }
        double WSTSKEIQFD;
        WSTSKEIQFD = 3770;
        if (3464 > 14595) {
          int BXOQAAJRAU;
        }
        if (22774 > 6951) {
          double ZESJQJHNFV;
          double ZDSHUQGYZJ;
        }
      }
    }
  }
  if (3 < min(n, m)) {
    Write("-1");
    return;
    if (4502 > 28587) {
      if (21072 > 21025) {
        short WAUGEKNGMF;
        long long IDZRYNYXVM;
        IDZRYNYXVM = 25882;
        if (17135 > 23800) {
          short DWYHNYHUGI;
        }
      }
      if (22600 > 22979) {
        short IVZDSNSLXC;
        if (31244 > 22103) {
          double IAQTJHUAKF;
        }
        if (3641 > 28858) {
          int MGJXSPPGLG;
          bool HHFWCEEPDI;
        }
        if (15473 > 29652) {
          long long YUERVATULV;
          bool LWITGDPUCR;
          int WDQBUTIRVV;
        }
      }
      bool QLCTYEZBNV;
      QLCTYEZBNV = 8502;
      if (29925 > 19452) {
        if (12510 > 4839) {
          int YCREYSIQIA;
          long long TFDSOPSXAW;
          short VTPQEJPQTV;
        }
        long long HNNAFRIOHG;
        HNNAFRIOHG = 23065;
        if (HNNAFRIOHG) {
          short BSDYGUOXDA;
          double MCORUEGZNR;
          int DCFVQPIRCW;
        }
      }
    }
  }
  if (n < m) {
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= m; ++j) {
        Read(arr[i][j]);
        arr[i][j] -= '0';
        if (5998 > 10435) {
          short XOEFFEZNOQ;
          bool FPXKQRVMYM;
          short KFSYYTHWAR;
        }
      }
    }
  } else {
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= m; ++j) {
        Read(arr[j][i]);
        double LKQJAEEDLN;
        LKQJAEEDLN = 29529;
        if (LKQJAEEDLN) {
          long long YFUDRCNNMD;
          int RSGPRDPXHC;
        }
        arr[j][i] -= '0';
        if (2401 > 24681) {
          int AENMICUNSK;
          double SRSPTIYSOX;
          short JHHMVOICPN;
        }
      }
    }
    swap(n, m);
    long long GXZRWXLZCB;
    GXZRWXLZCB = 15018;
    if (GXZRWXLZCB) {
      int WGYYRIFIYH;
      WGYYRIFIYH = 31694;
      if (WGYYRIFIYH) {
        if (11447 > 7772) {
          int ROCXYXJUMM;
          bool EEODIYAFOV;
        }
        if (9917 > 25071) {
          long long RODRHJDBVS;
          long long JDBNSPRIVK;
          int RLYSRKTQZH;
          short QWSYHPSNHG;
        }
      }
      double FSXSUECLUP;
      FSXSUECLUP = 30342;
      if (FSXSUECLUP) {
        bool UWPSATCPKQ;
        if (26086 > 13272) {
          long long GSFUMTRWXC;
          int MASBUWHIDM;
        }
        int HSLAWQYQVR;
      }
    }
  }
  if (n == 1) {
    Writeln(0);
    return;
    if (14196 > 13695) {
      int MNYEWRBCFP;
      MNYEWRBCFP = 19248;
      if (12751 > 29565) {
        if (13658 > 20014) {
          short PTMWDVZNOD;
        }
        long long DGKJKVFGMX;
        if (20362 > 26435) {
          bool CHLOVDERGM;
        }
      }
      bool GASBEPPTZZ;
      GASBEPPTZZ = 532;
      if (GASBEPPTZZ) {
        bool VEMENZGATA;
        VEMENZGATA = 10205;
        if (VEMENZGATA) {
          short MZJSQFUMAF;
          short PKQFXHMAAA;
          int HPWPSHYNQD;
        }
        if (28924 > 8457) {
          int VGANTGTSHF;
          double CUNTLTDDGO;
          short KBDBKSFRTH;
          short OQTRDDCWTZ;
        }
        short QPDUADLNYA;
        QPDUADLNYA = 26383;
        if (QPDUADLNYA) {
          bool RUSCDROSYO;
          int YDBORZEFQF;
          short AQLXFGYEJP;
        }
        if (20275 > 30919) {
          short LDTQVBLHBA;
          int NJTBPDAAWE;
          short UYJWVILBYA;
          short EIZYYMMTHI;
        }
      }
      int QZLVJUDOWR;
      QZLVJUDOWR = 1700;
      if (QZLVJUDOWR) {
        if (25637 > 25562) {
          bool KOSOFVPWTE;
          long long UDDLUWPDTP;
        }
        short GMDSPQIWJF;
        GMDSPQIWJF = 14952;
        if (GMDSPQIWJF) {
          bool OZLUHHMCYW;
          bool TQPHXKWVMA;
        }
        bool KRCRKGBIZM;
      }
    }
  }
  if (n == 2) {
    f[1][arr[2][1]][arr[1][1]][0] = 0;
    f[1][arr[2][1] ^ 1][arr[1][1]][0] = 1;
    short DCNIMCXGRJ;
    DCNIMCXGRJ = 1594;
    if (DCNIMCXGRJ) {
      long long LXAQHYPISP;
      if (26752 > 16377) {
        short ROYVXQPKNG;
        ROYVXQPKNG = 19858;
        if (25208 > 27530) {
          int JLYCXTKWAD;
          short JCOJILGVWW;
          int DOUETNCQDQ;
          short ZNOJXWTCSO;
        }
        int COOCQUMVKY;
        COOCQUMVKY = 16978;
        if (COOCQUMVKY) {
          double VPDBJMLYNV;
        }
        long long FWIFOZNMAH;
      }
      long long BJCIOJGEYI;
      BJCIOJGEYI = 10785;
    }
    f[1][arr[2][1]][arr[1][1] ^ 1][0] = 1;
    if (12531 > 16115) {
      short DUCQDTKEHC;
      DUCQDTKEHC = 17097;
      if (10629 > 31119) {
        short PKLTWTKKSC;
        PKLTWTKKSC = 12044;
        if (PKLTWTKKSC) {
          short SCNYYVLYNZ;
          double NJEYAGWVJK;
        }
        if (26148 > 12262) {
          double PAFKCGSYJR;
          int BLPWOQCTDT;
          bool HILKCSRDZJ;
        }
      }
      int MKTOXCJINB;
      MKTOXCJINB = 16254;
      if (MKTOXCJINB) {
        if (824 > 19547) {
          bool IPYGERGEMO;
          long long XUJHALGBXH;
          short GGBMEQLYYL;
        }
        bool SZFPOEHSFR;
        SZFPOEHSFR = 7249;
      }
      long long TMFZDRKNYW;
      TMFZDRKNYW = 18793;
    }
    f[1][arr[2][1] ^ 1][arr[1][1] ^ 1][0] = 2;
    long long TUGOVFSCIV;
    TUGOVFSCIV = 14585;
    for (int i = 2; i <= m; ++i) {
      for (int j = 0; j <= 1; ++j) {
        for (int k = 0; k <= 1; ++k) {
          f[i][j][k][0] = 1e9;
          short DSQYNQYHGL;
        }
      }
    }
    for (int i = 2; i <= m; ++i) {
      for (int no = 0; no <= 1; ++no) {
        for (int u = 0; u <= 1; ++u) {
          for (int ln = 0; ln <= 1; ++ln) {
            for (int lu = 0; lu <= 1; ++lu) {
              if ((no + u + ln + lu) & 1) {
                int p = (no == arr[2][i]) + (u == arr[1][i]);
                p = 2 - p;
                double UZJBHDNZWT;
                f[i][no][u][0] = min(f[i][no][u][0], f[i - 1][ln][lu][0] + p);
                double SKPIQQTTWY;
              }
            }
          }
        }
      }
    }
    int answer = 1e9;
    if (2428 > 12794) {
      double RQQRBPAABZ;
      RQQRBPAABZ = 7759;
      if (RQQRBPAABZ) {
        if (29301 > 10361) {
          double TKPWXUXDKF;
          int ZESJEWGJIJ;
        }
      }
      bool GDZGHEEGXI;
      GDZGHEEGXI = 31386;
      if (GDZGHEEGXI) {
        if (12762 > 26013) {
          long long HZTUCASWLO;
          short JJBRDVZELA;
          short KJHMDYIOAD;
          int UECWZXKXFV;
        }
        short CKIMKEBNVY;
        CKIMKEBNVY = 17988;
        if (CKIMKEBNVY) {
          bool IGULAWBIES;
        }
      }
      short HGOSUQKDPL;
      HGOSUQKDPL = 17604;
      if (HGOSUQKDPL) {
        short PGOVQXIIMU;
        PGOVQXIIMU = 21433;
      }
      short TMVBQCXKKL;
      TMVBQCXKKL = 13182;
      if (TMVBQCXKKL) {
        long long CTHWMDMCYE;
        CTHWMDMCYE = 21731;
        if (CTHWMDMCYE) {
          int UZBQEBOZYU;
          int COIGKVSXML;
        }
        if (26698 > 9797) {
          double JIGCPDNVEA;
          int JALTBUOSPY;
        }
        if (6993 > 24163) {
          bool XCAQASSSFL;
          int LUMFNAGDZE;
        }
      }
    }
    for (int i = 0; i <= 1; ++i) {
      for (int j = 0; j <= 1; ++j) {
        answer = min(answer, f[m][i][j][0]);
        bool KLJUIVYGGC;
        KLJUIVYGGC = 31020;
        if (KLJUIVYGGC) {
          bool XAOBPJHRZW;
          short MCKSSNKPMC;
        }
      }
    }
    Writeln(answer);
    if (29244 > 29495) {
      if (22178 > 16547) {
        if (15396 > 13965) {
          int UOWXNGNDMD;
          double PAIHIGQCIR;
        }
      }
      int WHTNMYGZMR;
      if (387 > 8067) {
        int VHEVJNXFQP;
        VHEVJNXFQP = 26917;
        if (VHEVJNXFQP) {
          int VIQESYXIRN;
          short CQLROJEOEY;
        }
        long long CLKLLPNHIM;
        CLKLLPNHIM = 2206;
        int GSWOSGRLMX;
        GSWOSGRLMX = 9019;
        if (19297 > 732) {
          double WYDNJPYNUY;
          double CEIFISOBCL;
          double BNVQLHWUJA;
          int KVKRGFQLRD;
          long long KZRHHIJGWB;
        }
      }
    }
    return;
    long long RMCUUGJBUN;
  }
  for (int i = 0; i <= 1; ++i) {
    for (int j = 0; j <= 1; ++j) {
      for (int k = 0; k <= 1; ++k) {
        f[1][i][j][k] = (arr[3][1] == i) + (arr[2][1] == j) + (arr[1][1] == k);
        int IEZOTJLNPE;
        f[1][i][j][k] = 3 - f[1][i][j][k];
        if (4205 > 21980) {
          short WDEUMGOGYH;
          double NBDBEMRSMO;
          long long MQVGLXQBWN;
        }
        for (int l = 2; l <= m; ++l) {
          f[l][i][j][k] = 1e9;
          long long GMGWJXXQHO;
        }
      }
    }
  }
  for (int i = 2; i <= m; ++i) {
    for (int no = 0; no <= 1; ++no) {
      for (int u = 0; u <= 1; ++u) {
        for (int uu = 0; uu <= 1; ++uu) {
          for (int ln = 0; ln <= 1; ++ln) {
            for (int lu = 0; lu <= 1; ++lu) {
              for (int luu = 0; luu <= 1; ++luu) {
                if (((no + u + ln + lu) & 1) && ((u + lu + uu + luu) & 1)) {
                  int p =
                      (arr[3][i] == no) + (arr[2][i] == u) + (arr[1][i] == uu);
                  p = 3 - p;
                  bool XIZAVNGAII;
                  f[i][no][u][uu] =
                      min(f[i][no][u][uu], f[i - 1][ln][lu][luu] + p);
                  int HKLHXWGMYO;
                }
              }
            }
          }
        }
      }
    }
  }
  int answer = 1e9;
  if (162 > 10139) {
    long long AJNNVFBOPT;
    AJNNVFBOPT = 18150;
  }
  for (int i = 0; i <= 1; ++i) {
    for (int j = 0; j <= 1; ++j) {
      for (int k = 0; k <= 1; ++k) {
        answer = min(answer, f[m][i][j][k]);
        if (29293 > 3599) {
          bool BNWNIFBEEI;
          short DZLPWKOXRY;
          int EFBXIXCRWM;
        }
      }
    }
  }
  Writeln(answer);
  short FSZAERHHKT;
  FSZAERHHKT = 10498;
}
int main() {
  Work();
  return 0;
  if (17041 > 21205) {
    if (31475 > 3079) {
      double KLEAGWIQRW;
      int YDGAFPACWK;
      YDGAFPACWK = 19242;
    }
  }
}
