type ji=^rec; rec=record data,v:longint; next:ji; end; var a:array[0..2000000] of ji; v:array[0..2000000] of boolean; d:array[0..2000000] of int64; q:array[0..3000000] of longint; h:array[0..1001,0..1001,0..1] of longint; i,j,k,m,n,vv,vs,vt:longint; procedure spfa; var p:ji; s,t,head,tail,i,now:longint; begin fillchar(d,sizeof(d),30); fillchar(v,sizeof(v),0); fillchar(q,sizeof(q),0); d[vs]:=0; v[vs]:=true; q[1]:=vs; head:=1; tail:=1; s:=1; t:=1; while s<=t do begin now:=q[head]; p:=a[now]; while p<>nil do begin i:=p^.data; if d[i]>d[now]+p^.v then begin d[i]:=d[now]+p^.v; if not v[i] then begin inc(tail); inc(t); if tail>3000000 then tail:=1; q[tail]:=i; v[i]:=true; end; end; p:=p^.next; end; inc(head); inc(s); if head>3000000 then head:=1; v[now]:=false; end; end; procedure insert(x,y,w:longint); var p:ji; begin new(p); p^.data:=y; p^.v:=w; p^.next:=a[x]; a[x]:=p; new(p); p^.data:=x; p^.v:=w; p^.next:=a[y]; a[y]:=p; end; begin readln(n,m); vv:=0; for i:=1 to n-1 do for j:=1 to m-1 do for k:=0 to 1 do begin inc(vv); h[i,j,k]:=vv; end; fillchar(a,sizeof(a),0); vs:=vv+1; vt:=vs+1; for i:=1 to m-1 do begin read(k); insert(vs,h[1,i,0],k); end; for i:=1 to n-2 do for j:=1 to m-1 do begin read(k); insert(h[i,j,1],h[i+1,j,0],k); end; for i:=1 to m-1 do begin read(k); insert(h[n-1,i,1],vt,k); end; for i:=1 to n-1 do begin read(k); insert(h[i,1,1],vt,k); for j:=1 to m-2 do begin read(k); insert(h[i,j,0],h[i,j+1,1],k); end; read(k); insert(vs,h[i,m-1,0],k); end; for i:=1 to n-1 do for j:=1 to m-1 do begin read(k); insert(h[i,j,0],h[i,j,1],k); end; spfa; writeln(d[vt]); end.